欧美第十页,AA视频aa,肏比视频网站,老鸭窝在线观看免费视频

formatdatetime(formatDateTime(new Date())日期不對)

前沿拓展:

formatdatetime

uses DateUtils;

procedure TForm1.Button1Click(Sender: TObject);
beg輕in
Label1.Caption:=FormatdateTime('dddddd ',now);
end;

procedure TForm1.Button2Click(Sender: TObject);
function 來自Replace(astr: string):string;
begin
astr:= StringReplac載e(astr,'年','-',[rfReplaceAll]) ;
astday( StrToDate(vstr), 1));
獲取小時時,有2個選項,1個返回12小時制的小時數(shù),1個返回24小時制的小時數(shù),因為現(xiàn)在是晚上8點,所以calendar.get(Calendar.HOUR)返回8,而calendar.get(Calendar.HOUR_OF_DAY)返回20。

如果使用java.time.LocalTime,那代碼是下面這樣的:

LocalTime localTime = LocalTime.now();
System.out.println("localTime:" + localTime);

int hour = localTime.getHour();
int minute = localTime.getMinute();
int second = localTime.getSecond();

System.out.println("hour: " + hour);
System.out.println("minute: " + minute);
System.out.println("second: " + second);

輸出結(jié)果:

formatdatetime(formatDateTime(new Date())日期不對)

可以看出,LocalTime只有時間沒有日期。

4. LocalDateTime4.1 獲取當(dāng)前時間LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime:" + localDateTime);

輸出結(jié)果:

localDateTime: 2020-06-11T11:03:21.376

4.2 獲取年月日時分秒LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime: " + localDateTime);

System.out.println("year: " + localDateTime.getYear());
System.out.println("month: " + localDateTime.getMonthValue());
System.out.println("day: " + localDateTime.getDayOfMonth());
System.out.println("hour: " + localDateTime.getHour());
System.out.println("minute: " + localDateTime.getMinute());
System.out.println("second: " + localDateTime.getSecond());

輸出結(jié)果:

formatdatetime(formatDateTime(new Date())日期不對)

4.3 增加天數(shù)/小時LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime: " + localDateTime);

LocalDateTime tomorrow = localDateTime.plusDays(1);
System.out.println("tomorrow: " + tomorrow);

LocalDateTime nextHour = localDateTime.plusHours(1);
System.out.println("nextHour: " + nextHour);

輸出結(jié)果:

localDateTime: 2020-06-11T11:13:44.979

tomorrow: 2020-06-12T11:13:44.979

nextHour: 2020-06-11T12:13:44.979

LocalDateTime還提供了添加年、周、分鐘、秒這些方法,這里就不一一列舉了:

formatdatetime(formatDateTime(new Date())日期不對)

4.4 減少天數(shù)/小時LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime: " + localDateTime);

LocalDateTime yesterday = localDateTime.minusDays(1);
System.out.println("yesterday: " + yesterday);

LocalDateTime lastHour = localDateTime.minusHours(1);
System.out.println("lastHour: " + lastHour);

輸出結(jié)果:

localDateTime: 2020-06-11T11:20:38.896

yesterday: 2020-06-10T11:20:38.896

lastHour: 2020-06-11T10:20:38.896

類似的,LocalDateTime還提供了減少年、周、分鐘、秒這些方法,這里就不一一列舉了:

formatdatetime(formatDateTime(new Date())日期不對)

4.5 獲取時間是本周/本年的第幾天LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime: " + localDateTime);

System.out.println("DayOfWeek: " + localDateTime.getDayOfWeek().getValue());
System.out.println("DayOfYear: " + localDateTime.getDayOfYear());

輸出結(jié)果:

localDateTime: 2020-06-11T11:32:31.731

DayOfWeek: 4

DayOfYear: 163

5. DateTimeFormatter

JDK8中推出了java.time.format.DateTimeFormatter來處理日期格式化問題,《阿里巴巴Java開發(fā)手冊》中也是建議使用DateTimeFormatter代替SimpleDateFormat。

5.1 格式化LocalDateLocalDate localDate = LocalDate.now();

System.out.println("ISO_DATE: " + localDate.format(DateTimeFormatter.ISO_DATE));
System.out.println("BASIC_ISO_DATE: " + localDate.format(DateTimeFormatter.BASIC_ISO_DATE));
System.out.println("ISO_WEEK_DATE: " + localDate.format(DateTimeFormatter.ISO_WEEK_DATE));
System.out.println("ISO_ORDINAL_DATE: " + localDate.format(DateTimeFormatter.ISO_ORDINAL_DATE));

輸出結(jié)果:

formatdatetime(formatDateTime(new Date())日期不對)

如果提供的格式無法滿足你的需求,你還可以像以前一樣自定義格式:

LocalDate localDate = LocalDate.now();

System.out.println("yyyy/MM/dd: " + localDate.format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));

輸出結(jié)果:

yyyy/MM/dd: 2020/06/11

5.2 格式化LocalTimeLocalTime localTime = LocalTime.now();
System.out.println(localTime);
System.out.println("ISO_TIME: " + localTime.format(DateTimeFormatter.ISO_TIME));
System.out.println("HH:mm:ss: " + localTime.format(DateTimeFormatter.ofPattern("HH:mm:ss")));

輸出結(jié)果:

14:28:35.230

ISO_TIME: 14:28:35.23

HH:mm:ss: 14:28:35

5.3 格式化LocalDateTimeLocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
System.out.println("ISO_DATE_TIME: " + localDateTime.format(DateTimeFormatter.ISO_DATE_TIME));
System.out.println("ISO_DATE: " + localDateTime.format(DateTimeFormatter.ISO_DATE));

輸出結(jié)果:

2020-06-11T14:33:18.303

ISO_DATE_TIME: 2020-06-11T14:33:18.303

ISO_DATE: 2020-06-11

6. 類型相互轉(zhuǎn)換6.1 Instant轉(zhuǎn)Date

JDK8中,Date新增了from()方法,將Instant轉(zhuǎn)換為Date,代碼如下所示:

Instant instant = Instant.now();
System.out.println(instant);

Date dateFromInstant = Date.from(instant);
System.out.println(dateFromInstant);

輸出結(jié)果:

2020-06-11T06:39:34.979Z

Thu Jun 11 14:39:34 CST 2020

6.2 Date轉(zhuǎn)Instant

JDK8中,Date新增了toInstant方法,將Date轉(zhuǎn)換為Instant,代碼如下所示:

Date date = new Date();
Instant dateToInstant = date.toInstant();
System.out.println(date);
System.out.println(dateToInstant);

輸出結(jié)果:

Thu Jun 11 14:46:12 CST 2020

2020-06-11T06:46:12.112Z

6.3 Date轉(zhuǎn)LocalDateTimeDate date = new Date();
Instant instant = date.toInstant();
LocalDateTime localDateTimeOfInstant = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
System.out.println(date);
System.out.println(localDateTimeOfInstant);

輸出結(jié)果:

Thu Jun 11 14:51:07 CST 2020

2020-06-11T14:51:07.904

6.4 Date轉(zhuǎn)LocalDateDate date = new Date();
Instant instant = date.toInstant();
LocalDateTime localDateTimeOfInstant = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
LocalDate localDate = localDateTimeOfInstant.toLocalDate();
System.out.println(date);
System.out.println(localDate);

輸出結(jié)果:

Thu Jun 11 14:59:38 CST 2020

2020-06-11

可以看出,Date是先轉(zhuǎn)換為Instant,再轉(zhuǎn)換為LocalDateTime,第二通過LocalDateTime獲取LocalDate。

6.5 Date轉(zhuǎn)LocalTimeDate date = new Date();
Instant instant = date.toInstant();
LocalDateTime localDateTimeOfInstant = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
LocalTime toLocalTime = localDateTimeOfInstant.toLocalTime();
System.out.println(date);
System.out.println(toLocalTime);

輸出結(jié)果:

Thu Jun 11 15:06:14 CST 2020

15:06:14.531

可以看出,Date是先轉(zhuǎn)換為Instant,再轉(zhuǎn)換為LocalDateTime,第二通過LocalDateTime獲取LocalTime。

6.6 LocalDateTime轉(zhuǎn)DateLocalDateTime localDateTime = LocalDateTime.now();

Instant toInstant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
Date dateFromInstant = Date.from(toInstant);
System.out.println(localDateTime);
System.out.println(dateFromInstant);

輸出結(jié)果:

2020-06-11T15:12:11.600

Thu Jun 11 15:12:11 CST 2020

6.7 LocalDate轉(zhuǎn)DateLocalDate today = LocalDate.now();

LocalDateTime localDateTime = localDate.atStartOfDay();
Instant toInstant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
Date dateFromLocalDate = Date.from(toInstant);
System.out.println(dateFromLocalDate);

輸出結(jié)果:

Thu Jun 11 00:00:00 CST 2020

6.8 LocalTime轉(zhuǎn)DateLocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();

LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
Instant instantFromLocalTime = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
Date dateFromLocalTime = Date.from(instantFromLocalTime);

System.out.println(dateFromLocalTime);

輸出結(jié)果:

Thu Jun 11 15:24:18 CST 2020

7. 小編綜合來說

JDK8推出了全新的日期時間類,如Instant、LocaleDate、LocalTime、LocalDateTime、DateTimeFormatter,設(shè)計比之前更合理,也是線程安全的。

《阿里巴巴Java開發(fā)規(guī)范》中也推薦使用Instant代替Date,LocalDateTime 代替 Calendar,DateTimeFormatter 代替 SimpleDateFormat。

因此,如果條件允許,建議在項目中使用,沒有使用的,可以考慮升級下。

拓展知識:

原創(chuàng)文章,作者:九賢生活小編,如若轉(zhuǎn)載,請注明出處:http://cxzzxj.cn/38101.html

中文字幕免费在线| 国产精品无码久久久久久免费| 孕交毛片| 99精品免视看| 公侵犯玩弄漂亮人妻优| 最新视频日韩无码| 免费五级片| 国产一区二区成人| 91精品国产1区2区3区| 亚洲色图视频二区| 国产精品免费不卡无码av| 欧洲丰满熟护士一区二区| 亚洲吧Av无码不卡一区二区三区| 无码高清一区二区三区四区| 欧美日韩中文字幕精品| 亚洲精品无码美利坚合众国在线| 淫乱丝袜人妻| 色天使婷婷综合久久| 中文字幕av高潮| 乱伦国产老熟女视频| 纯肉无码动画88AV| 理论无码免费| 久久精品国产亚洲AV无码4区| www.av夜爽| 性导航亚洲| 一级毛色毛片无码免费片| 亚洲Av一成人之国产精品| 久久99久久久久婷婷| 成人午夜一级网| 2020久久精品视频| 影院久久999精品久久久久久| 91av视频乱伦| 91精品国产91久久久久蜜臀 | 婷婷五月天在线中文| 久久免费看黄a级毛片| 欧美一区二区黄色电影| 精品少妇牲交视频大全| 天天午夜玖玖网站美国| 亚洲精品无码AV片| 最新国产在线不卡AV| 亚洲精品乱码久久久久久动图|