java怎么获取当前时间
3个回答
展开全部
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
展开全部
/**
* 获取系统当前时间 <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;
}
* 获取系统当前时间 <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;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
system.currenttimemillis()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询