Calendar 和Date得到當前時間的年月日

2021-05-01 00:22:51 字數 943 閱讀 2636

1.calendar calendar = calendar.getinstance();

string year = string.valueof(calendar.get(1));

string month = string.valueof(calendar.get(2) + 1);

if(month.length() == 1)

month = "0" + month;

string date = string.valueof(calendar.get(5));

if(date.length() == 1)

date = "0" + date;

string hour = string.valueof(calendar.get(11));

if(hour.length() == 1)

hour = "0" + hour;

string minute = string.valueof(calendar.get(12));

if(minute.length() == 1)

minute = "0" + minute;

string second = string.valueof(calendar.get(13));

if(second.length() == 1)

second = "0" + second;

system.out.println(year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second);

2.   date d=new date();

******dateformat   sdf   =   new   ******dateformat( "yyyy-mm-dd hh:mm:ss");

system.out.println(sdf.format(d));

Calendar和Date的用法

1 用calendar將2001年3月15號 15點37分5秒 格式化為毫秒數 public static void main string args throws parseexception calendar calendar calendar.getinstance calendar.set ...

java的Calendar和Date例項

最近看了看關於時間獲取,calendar和date類,小了個例子涵蓋大部分方法及常量,可執行對比。計算2013 01 01的資訊 private static void theday 上月1號的一些資訊 private static void lastmonfirday cal calendar.g...

Date和Calendar相關API知識點

獲取當前系統時間 獲取當前系統時間毫秒數 long now system.currenttimemillis system.out.println now 1511331950177 long year now 1000 60 60 24 365 1970 system.out.println ye...