java如何获取当前时间 年月日 时分秒

Python012

java如何获取当前时间 年月日 时分秒,第1张

//得到long类型当前时间

long l = System.currentTimeMillis()

//new日期对

Date date = new Date(l)

//转换提日期输出格式

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-

dd HH:mm:ss")System.out.println(dateFormat.format(date))

扩展资料

package com.ob

import java.text.ParseException

import java.text.SimpleDateFormat

import java.util.Calendar

import java.util.Date

public class DateTest {

public static void main(String[] args) throws ParseException {

Calendar now = Calendar.getInstance()

System.out.println("年: " + now.get(Calendar.YEAR))

System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "")

System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH))

System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY))

System.out.println("分: " + now.get(Calendar.MINUTE))

System.out.println("秒: " + now.get(Calendar.SECOND))

System.out.println("当前时间毫秒数:" + now.getTimeInMillis())

System.out.println(now.getTime())

\t\tDate d = new Date()

System.out.println(d)

\t\tSimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

\t\tString dateNowStr = sdf.format(d)

System.out.println("格式化后的日期:" + dateNowStr)

\t\t

\t\tString str = "2012-1-13 17:26:33"

//要跟上面sdf定义的格式一样

\t\tDate today = sdf.parse(str)

System.out.println("字符串转成日期:" + today)

\t}

}

参考资料:Java - 百度百科

1、获取当前的时间

Date date=new Date()//此时date为当前的时间

2、设置时间的格式

Date date=new Date()//此时date为当前的时间

System.out.println(date)

SimpleDateFormat dateFormat=new SimpleDateFormat(“YYYY-MM-dd”)//设置当前时间的格式,为年-月-日

System.out.println(dateFormat.format(date))

SimpleDateFormat dateFormat_min=new SimpleDateFormat(“YYYY-MM-dd HH:mm:ss”)//设置当前时间的格式,为年-月-日 时-分-秒

System.out.println(dateFormat_min.format(date))

扩展资料

java 获取当前微秒时间:

package com.ffcs.itm

public class DataSecUtils {

public static void main(String[] args) {

System.out.println(System.currentTimeMillis())// 毫秒

System.out.println(getmicTime())

System.out.println(System.currentTimeMillis())// 毫秒

System.out.println(getmicTime())

}

/**

* @return返回微秒

*/

public static Long getmicTime() {

Long cutime = System.currentTimeMillis() * 1000// 微秒

Long nanoTime = System.nanoTime()// 纳秒

return cutime + (nanoTime - nanoTime / 1000000 * 1000000) / 1000

}

}

使用字符串格式化函数就可以了,例如:

simpledateformat

dataformat=new

simpledateformat("yyyy-mm-dd

hh:mm:ss")

string

datestr=

dataformat.format(new

date())

就会输出年、月、日、时、分、秒了

标志符为:

g年代

y

m

d

h

时在上午或下午

(1~12)

h

时在一天中

(0~23)

m

s

s

毫秒

e

星期

d

一年中的第几天

f

一月中第几个星期几

w

一年中第几个星期

w

一月中第几个星期

a

上午

/

下午标记符

k

时在一天中

(1~24)

k

时在上午或下午

(0~11)

z

时区