damoshayu.cn,苍井空浴缸大战猛男120分钟,网址你懂的,中国女人内射6XXXXXWWW

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

前沿拓展:

formatdatetime

uses DateUtils;

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

procedure TForm1.Button2Click(Sender: TObject);
function 來(lái)自Replace(astr: string):string;
begin
astr:= StringReplac載e(astr,'年','-',[rfReplaceAll]) ;
astday( StrToDate(vstr), 1));
獲取小時(shí)時(shí),有2個(gè)選項(xiàng),1個(gè)返回12小時(shí)制的小時(shí)數(shù),1個(gè)返回24小時(shí)制的小時(shí)數(shù),因?yàn)楝F(xiàn)在是晚上8點(diǎn),所以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())日期不對(duì))

可以看出,LocalTime只有時(shí)間沒(méi)有日期。

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

輸出結(jié)果:

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

4.2 獲取年月日時(shí)分秒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())日期不對(duì))

4.3 增加天數(shù)/小時(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())日期不對(duì))

4.4 減少天數(shù)/小時(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())日期不對(duì))

4.5 獲取時(shí)間是本周/本年的第幾天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來(lái)處理日期格式化問(wèn)題,《阿里巴巴Java開(kāi)發(fā)手冊(cè)》中也是建議使用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())日期不對(duì))

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

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)換為L(zhǎng)ocalDateTime,第二通過(guò)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)換為L(zhǎng)ocalDateTime,第二通過(guò)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. 小編綜合來(lái)說(shuō)

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

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

因此,如果條件允許,建議在項(xiàng)目中使用,沒(méi)有使用的,可以考慮升級(jí)下。

拓展知識(shí):

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