Java Calendar獲取年 月 日 時間

2021-06-20 10:17:04 字數 1802 閱讀 7392

calendar c = calendar.getinstance(timezone.gettimezone("gmt+08:00")); //獲取東八區時間

int year = c.get(calendar.year); //獲取年

int month = c.get(calendar.month) + 1; //獲取月份,0表示1月份

int day = c.get(calendar.day_of_month); //獲取當前天數

int first = c.getactualminimum(c.day_of_month); //獲取本月最小天數

int last = c.getactualmaximum(c.day_of_month); //獲取本月最大天數

int time = c.get(calendar.hour_of_day); //獲取當前小時

int min = c.get(calendar.minute); //獲取當前分鐘

int xx = c.get(calendar.second); //獲取當前秒

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

system.out.println("當前日期curdate====:" + curdate);

輸出結果:

當前日期curdate:2012-09-25 22:50:54

calendar的計算

輸出結果:

獲取上個月的年、月、日

calendar c=calendar.getinstance();

c.add(calendar.month, -1);//上個月

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

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

string topday = string.valueof(c.getactualminimum(calendar.day_of_month));

string lastday = string.valueof(c.getactualmaximum(calendar.day_of_month));

//上個月

string lastmonth=string.valueof(c.get(calendar.month)+1).length()==2?string.valueof(c.get(calendar.month)+1):"0"+string.valueof(c.get(calendar.month)+1);

string topdaymonth=year+"-"+lastmonth+"-"+"01" + " 00:00:00";

string lastdaymonth = year+"-"+lastmonth+"-"+lastday+ " 23:59:59";

system.out.println("###year:" + year);

system.out.println("###last month:" + lastmonth);

system.out.println("###topday:" + topdaymonth);

system.out.println("###lastday:" + lastdaymonth);

輸出結果

###year:2013

###last month:04

###topday:2013-04-01 00:00:00

###lastday:2013-04-30 23:59:59

Java Calendar獲取時間的月和日 周

calendar cal calendar.getinstance 當前年 int year cal.get calendar.year 當前月 int month cal.get calendar.month 1 當前月的第幾天 即當前日 int day of month cal.get cale...

java Calendar學習筆記

calendar 與 date 的轉換非常簡單 calendar calendar calendar.getinstance 從乙個 calendar 物件中獲取 date 物件 date date calendar.gettime 將 date 物件反應到乙個 calendar 物件中,calen...

Java Calendar使用總結

dateformat sdf new dateformat yyyy mm dd hh mm ss sss 初始化 calendar cal calendar.getinstance int year cal.get calendar.year 年 int month cal.get calenda...