關於時間的各種操作

2021-09-29 07:14:51 字數 4219 閱讀 6711

日期字串格式大小比較

string str1="2015-02-08";

string str2="2015-01-08";

int res=str1.compareto(str2);

1是大於 0是等於 -1是小於

獲取某年第一天日期

calendar calendar = calendar.getinstance();

calendar.clear();

calendar.set(calendar.year, year);

date curryearfirst = calendar.gettime();

獲取某年最後一天日期

calendar calendar = calendar.getinstance();

calendar.clear();

calendar.set(calendar.year, year);

calendar.roll(calendar.day_of_year, -1);

date curryearlast = calendar.gettime();

獲取當月首日

方1:calendar c = calendar.getinstance();

c.settime(date);

c.set(calendar.day_of_month, 1);

date = c.gettime();

return date;

方2:public static final ******dateformat sdf = new ******dateformat("yyyy-mm-dd");

//獲取當前月第一天:

calendar c = calendar.getinstance();

c.add(calendar.month, 0);

c.set(calendar.day_of_month, 1);//設定為1號,當前日期既為本月第一天

string monthfirst = timeutils.sdf.format(c.gettime());

system.out.println("***************nowfirst:" + monthfirst);

獲取當月最後一天

方1//獲取當前月最後一天

calendar ca = calendar.getinstance();

ca.set(calendar.day_of_month, ca.getactualmaximum(calendar.day_of_month));

string monthlast = timeutils.sdf.format(ca.gettime());

system.out.println("***************last:" + monthlast);

localdate date = localdate.of(2014, 3, 18);// 2014-03-18

int yeat = date.getyear();// 2014

month month = date.getmonth();// march

int day = date.getdayofmonth();// 18

dayofweek dow = date.getdayofweek();// tuesday

int len = date.getlengthofmonth();// 31

boolean leap = date.isleapyear();// false

localdate date1 = localdate.of(2014, 3, 18);// 2014-03-18

localdate date2 = date1.withtear(2011);// 2011-03-18

localdate date3 = date2.withdayofmonth(25);// 2011-03-25

localdate date4 = date3.with(chronofield.month_of_tear, 9)// 2011-09-25

localdate date1 = localdate.of(2014, 3, 18);// 2014-03-18

localdate date2 = date1.plusweeks(1);// 2014-03-25

localdate date3 = date2.minusyears(3);// 2011-03-25

localdate date4 = date3.plus(6, chronounit.months);)// 2011-09-25

localdate date = localdate.now();

int year = date.get(chronofield.year);

int month = date.get(chronofield.month_of_year);

int day = date.get(chronofield.day_of_month);

增減日/月

localdate now = localdate.now();

localdate minusmonthsafter = now.minusmonths(8);//操作月

localdate minusdaysafter = now.plusdays(-233);//操作日

string strday = minusdaysafter.format(datetimeformatter.ofpattern("yyyy-mm-dd"));//格式化

獲取當月首日

localdate today = localdate.now();

localdate kssj = today.with(temporaladjusters.firstdayofmonth());

string format = kssj.format(datetimeformatter.ofpattern("yyyy-mm-dd"));

system.out.println("獲取首日--"+format);

獲取當月最後一天

localdate today1 = localdate.now();

localdate kssj1 = today1.with(temporaladjusters.lastdayofmonth());

string format1 = kssj1.format(datetimeformatter.ofpattern("yyyy-mm-dd"));

system.out.println("獲取最後日--"+format1);

表示一天中的時間,可以使用localtime:

localtime time = localtime.of(13, 45, 20);// 12:45:20

int hour = time.gethour();// 13

int minute = time.getminute();// 45

int second = time.getsecond();// 20

localdate date = localdate.parse("2014-03-18");

localtime time = localtime.parse("13:45:20");

localdatetime是localdate和localtime的合體,同時表示了日期和時間,但是不帶有時區資訊。

// 2014-03-18t13:45:20

localdatetime dt1 = localdatetime.of(2014, month.march, 18, 13, 45, 20);

localdatetime dt2 = localdatetime.of(date, time);

localdatetime dt3 = date.attime(13, 45, 20);

localdatetime dt3 = time.atdate(date);

localdate date1 = dt1.tolocaldate();

localtime time1 = dt1.tolocaltime();

Oracle關於時間的各種處理

v base time0 date v base time date v starttime number 10 begin v base time0 to date 1970 01 01 00 00 00 yyyy mm dd hh24 mi ss v base time to date to c...

Mysql關於時間的操作

mysql查詢本季度 今天 select from ticket order detail where to days use time to days now 7天 select from ticket order detail where date sub curdate interval 7 ...

關於日期時間的操作

時間格式過濾器 vue.filter formatdate data 用法 new date gettime math.round math.random 100001.math.random 結果為0 1間的乙個隨機數 包括0,不包括1 2.math.floor num 向下取整 3.math.r...