java怎么获取当前时间

如题所述

JAVA中获取当前系统时间关键代码:

//得到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 {
        //得到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));
    }
}

输出结果:

2017-01-06 12:28:19

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-10-26
/**
* 获取系统当前时间 <br>
* 方 法 名:getCurrentDate<br>
*
* @param formatStr
* 需要格式的目标字符串例:yyyy-MM-dd
* @return Date 时间对象
*/
publicstatic Date getCurrentDate() {
returnnew Date();
}

publicString getTodayString() {
Calendarca = Calendar.getInstance();
StringcurrDate = ca.get(Calendar.YEAR) + "-"
+(ca.get(Calendar.MONTH) + 1) + "-"
+ca.get(Calendar.DAY_OF_MONTH);
intweek = ca.get(Calendar.DAY_OF_WEEK);
Stringweekday = "";
if(week == 1) {
weekday= "星期天";
}else if (week == 2) {
weekday= "星期一";
}else if (week == 3) {
weekday= "星期二";
}else if (week == 4) {
weekday= "星期三";
}else if (week == 5) {
weekday= "星期四";
}else if (week == 6) {
weekday= "星期五";
}else if (week == 7) {
weekday= "星期六";
}
returncurrDate + " " + weekday;
}本回答被提问者和网友采纳
第2个回答  2014-10-26
system.currenttimemillis()
相似回答