java 獲取當前年份 月份,當月第一天和最後一天

2021-08-26 05:31:30 字數 1849 閱讀 5205

public static void main(string args) {

// 獲取當前年份、月份、日期

calendar cale = null;

cale = calendar.getinstance();

int year = cale.get(calendar.year);

int month = cale.get(calendar.month) + 1;

int day = cale.get(calendar.date);

int hour = cale.get(calendar.hour_of_day);

int minute = cale.get(calendar.minute);

int second = cale.get(calendar.second);

int dow = cale.get(calendar.day_of_week);

int dom = cale.get(calendar.day_of_month);

int doy = cale.get(calendar.day_of_year);

system.out.println("current date: " + cale.gettime());

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

system.out.println("month: " + month);

system.out.println("day: " + day);

system.out.println("hour: " + hour);

system.out.println("minute: " + minute);

system.out.println("second: " + second);

system.out.println("day of week: " + dow);

system.out.println("day of month: " + dom);

system.out.println("day of year: " + doy);

// 獲取當月第一天和最後一天

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

string firstday, lastday;

// 獲取前月的第一天

cale = calendar.getinstance();

cale.add(calendar.month, 0);

cale.set(calendar.day_of_month, 1);

firstday = format.format(cale.gettime());

// 獲取前月的最後一天

cale = calendar.getinstance();

cale.add(calendar.month, 1);

cale.set(calendar.day_of_month, 0);

lastday = format.format(cale.gettime());

system.out.println("本月第一天和最後一天分別是 : " + firstday + " and " + lastday);

// 獲取當前日期字串

date d = new date();

system.out.println("當前日期字串1:" + format.format(d));

system.out.println("當前日期字串2:" + year + "/" + month + "/" + day + " "

+ hour + ":" + minute + ":" + second);

JS 獲取當前年份,月份

function dohandledate return tyear m function dohandleyear tyear function dohandlemonth return m 獲取完整的日期 var date new date var year date.getfullyear v...

如何使用PHP獲取當前年份?

如果您的伺服器支援短標籤,或者您使用php 5.4,則可以使用 隨著php朝著更加物件導向的方向發展,我很驚訝這裡沒有人引用內建的datetime類 now new datetime year now format y 或者在例項化時使用類成員訪問的單行 php 5.4 year new datet...

js獲取當前時間是年份或月份

1 js獲取兩個日期之間相差的天數 function getdaysbetween datestring1,datestring2 2 js獲取當前時間是年份或月份 var mydate new date mydate.getyear 獲取當前年份 2位 mydate.getfullyear 獲取完...