Calendar和Date的用法

2021-07-11 18:48:54 字數 1399 閱讀 1016

1)用calendar將2023年3月15號 15點37分5秒 格式化為毫秒數:

public static void main(string args) throws parseexception {

calendar calendar = calendar.getinstance();

calendar.set(2001, 2, 15, 03, 37, 5);

long time1 = calendar.gettimeinmillis();

system.out.println(time1);}}

2)用date將2023年3月15號 15點37分5秒 格式化為毫秒數:

public static void main1(string args) throws parseexception {

string date = "2001-03-15 15-37-05";

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

long time2 = ******dateformat.parse(date).gettime();

system.out.println(time2);}}

3)calendar和date相互轉換:

date date2 = new date(time1);

system.out.println(******dateformat.format(date2));

4)date型別的當前毫秒數轉換當前時間

******dateformat ******dateformat = new ******dateformat("hh:mm"+"\r\n" +"yyy/mm/dd");

date date = new date(system.currenttimemillis());

string timestring = ******dateformat.format(date);

system.out.println(timestring);

5)calendar型別的當前毫秒數轉換當前時間

calendar calendar = calendar.getinstance();

calendar.settimeinmillis(system.currenttimemillis());

system.out.println(calendar.get(calendar.hour_of_day)+":"+ calendar.get(calendar.millisecond)+"\r\n"+calendar.get(calendar.year)+

"/"+calendar.get(calendar.march)+"/"+calendar.get(calendar.day_of_month));

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...

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

1.calendar calendar calendar.getinstance string year string.valueof calendar.get 1 string month string.valueof calendar.get 2 1 if month.length 1 mont...