QQ网名大全

j**a如何获取当前时间 年月日 时分秒

//得到long类型当前时间

long l = system.currenttimemill**();

//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 j**a.text.parseexception;

import j**a.text.simpledateformat;

import j**a.util.calendar;

import j**a.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.gettimeinmill**()); 

system.out.println(now.gettime());

date d = new date(); 

system.out.println(d);
simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
string datenowstr = sdf.format(d); 

system.out.println("格式化后的日期:" + datenowstr);

string str = "2012-1-13 17:26:33";

//要跟上面sdf定义的格式一样
date today = sdf.parse(str); 

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

参考资料:j**a - 百科

佚名
2024-05-20 09:31:16
最佳回答
类似问题(10)