ڵλãҳ >> >> Java >>

ΪʲôʹDateʹJava8µʱ

ʱ䣺2019-9-16 9:51:58

ʾJava 8֮ǰйʱڵAPIڸʹ÷ȱݣ˽ʹµʱAPIֱӾɵʱڵAPIȱԼJava 8 µʱAPIн⡣1...


Java 8֮ǰйʱڵAPIڸʹ÷ȱݣ˽ʹµʱAPIֱӾɵʱڵAPIȱԼJava 8 µʱAPIн⡣

1|1ɵʱڵAPIȱ


Java java.util.Date java.util.Calendar Բ֧ʱҶ̰߳ȫġ

DateʽӡڿɶԲ

Thu Sep 12 13:47:34 CST 2019

ʹ SimpleDateFormat ʱиʽ SimpleDateFormat ̲߳ȫģSimpleDateFormat format Դ£

private StringBuffer format(Date date, StringBuffer toAppendTo, FieldDelegate delegate) { // Convert input date to time field list calendar.setTime(date); boolean useDateFormatSymbols = useDateFormatSymbols(); for (int i = 0; i < compiledPattern.length; ) { int tag = compiledPattern[i] >>> 8; int count = compiledPattern[i++] & 0xff; if (count == 255) { count = compiledPattern[i++] << 16; count |= compiledPattern[i++]; } switch (tag) { case TAG_QUOTE_ASCII_CHAR: toAppendTo.append((char)count); break; case TAG_QUOTE_CHARS: toAppendTo.append(compiledPattern, i, count); i += count; break; default: subFormat(tag, count, delegate, toAppendTo, useDateFormatSymbols); break; } } return toAppendTo; }

calendar ǹṵ̂߳ȫơ߳ͬʱʹͬ SimpleDateFormat staticε SimpleDateFormat formatʱ̻߳ͬʱ calendar.setTime һ̸߳ú time ֵһ߳ϰõ time ֵ޸˵·صĸʽʱǴġ

ڶಢʹ SimpleDateFormat ע⡣

SimpleDateFormat format ̲߳ȫ⣬parse Ҳ̲߳ȫġparse ʵʵ alb.establish(calendar).getTime() alb.establish(calendar) Ҫ

  • ڶcalֵ
  • ʹcalbcal
  • úõcal

ԭӲ½ʱǴġ

Dateʱ䴦Ƚ鷳ȡijꡢij¡ijڣԼ n Ժʱ䣬DateĻ̫ˣ Date getYeargetMonth Щˡ

1|2̲߳α֤̰߳ȫ


֮߳乲һ SimpleDateFormat ÿ߳ʹʱһ SimpleDateFormat => ٶĿ

ʹ format parse ĵطм => ߳ܲ

ʹ ThreadLocal ֤ÿֻ߳һ SimpleDateFormat => Ϻõķ

1|3Java 8 µʱAPI


Java 8ںʱ LocalDateLocalTimeInstantDuration Լ PeriodЩ඼ java.time УJava 8 µʱAPIʹ÷ʽʽ㡢޸ģǿȥʹá

LocalDate ֻȡ

// LocalDate // ȡǰ LocalDate localDate = LocalDate.now(); // ָ LocalDate localDate1 = LocalDate.of(2019, 9, 12); // ȡꡢ¡աڼ int year = localDate.getYear(); int year1 = localDate.get(ChronoField.YEAR); Month month = localDate.getMonth(); int month1 = localDate.get(ChronoField.MONTH_OF_YEAR); int day = localDate.getDayOfMonth(); int day1 = localDate.get(ChronoField.DAY_OF_MONTH); DayOfWeek dayOfWeek = localDate.getDayOfWeek(); int dayOfWeek1 = localDate.get(ChronoField.DAY_OF_WEEK);

LocalTime ֻȡʱ

// LocalTime LocalTime localTime = LocalTime.of(14, 14, 14); LocalTime localTime1 = LocalTime.now(); // ȡСʱ int hour = localTime.getHour(); int hour1 = localTime.get(ChronoField.HOUR_OF_DAY); // ȡ int minute = localTime.getMinute(); int minute1 = localTime.get(ChronoField.MINUTE_OF_HOUR); // ȡ int second = localTime.getMinute(); int second1 = localTime.get(ChronoField.SECOND_OF_MINUTE);

LocalDateTime ȡʱ룬൱ LocalDate + LocalTime

// LocalDateTime LocalDateTime localDateTime = LocalDateTime.now(); LocalDateTime localDateTime1 = LocalDateTime.of(2019, Month.SEPTEMBER, 10, 14, 46, 56); LocalDateTime localDateTime2 = LocalDateTime.of(localDate, localTime); LocalDateTime localDateTime3 = localDate.atTime(localTime); LocalDateTime localDateTime4 = localTime.atDate(localDate); // ȡLocalDate LocalDate localDate2 = localDateTime.toLocalDate(); // ȡLocalTime LocalTime localTime2 = localDateTime.toLocalTime();

Instant ȡڱʾһʱȷ룩

ֻΪ˻ȡߺʹ System.currentTimeMillis()

// Instant Instant instant = Instant.now(); // ȡ long currentSecond = instant.getEpochSecond(); // ȡ long currentMilli = instant.toEpochMilli();

Duration ʾһʱ

// Duration.between() Duration LocalDateTime from = LocalDateTime.of(2017, Month.JANUARY, 1, 00, 0, 0); // 2017-01-01 00:00:00 LocalDateTime to = LocalDateTime.of(2019, Month.SEPTEMBER, 12, 14, 28, 0); // 2019-09-15 14:28:00 Duration duration = Duration.between(from, to); // ʾ from to ʱ long days = duration.toDays(); // ʱ long hours = duration.toHours(); // ʱСʱ long minutes = duration.toMinutes(); // ʱķ long seconds = duration.getSeconds(); // ʱ long milliSeconds = duration.toMillis(); // ʱĺ long nanoSeconds = duration.toNanos(); // ʱ

޸ LocalDateLocalTimeLocalDateTimeInstant

LocalDateLocalTimeLocalDateTimeInstant Ϊɱ޸Щ᷵һ

ӡȣLocalDateTimeΪ:

LocalDateTime localDateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 12, 14, 32, 0); // һ localDateTime = localDateTime.plusYears(1); localDateTime = localDateTime.plus(1, ChronoUnit.YEARS); // һ localDateTime = localDateTime.minusMonths(1); localDateTime = localDateTime.minus(1, ChronoUnit.MONTHS); // ͨwith޸ijЩֵ // ޸Ϊ2020 localDateTime = localDateTime.withYear(2020); localDateTime = localDateTime.with(ChronoField.YEAR, 2020); // ʱ // ȡĵһ LocalDate localDate = LocalDate.now(); LocalDate localDate1 = localDate.with(firstDayOfYear());

TemporalAdjusters ྲֱ̬ӵãоһЩ

dayOfWeekInMonth ͬһÿܵĵڼ
firstDayOfMonth صµĵһ
firstDayOfNextMonth µĵһ
firstDayOfNextYear һĵһ
firstDayOfYear رĵһ
firstInMonth ͬһеһڼ
lastDayOfMonth صµһ
lastDayOfNextMonth µһ
lastDayOfNextYear һһ
lastDayOfYear رһ
lastInMonth ͬһһڼ
next / previous غһ/ǰһڼ
nextOrSame / previousOrSame غһ/ǰһڼֱֵӷ

ʽʱ

LocalDate localDate = LocalDate.of(2019, 9, 12); String s1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE); String s2 = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE); // Զʽ DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); String s3 = localDate.format(dateTimeFormatter);

ʱ

LocalDate localDate1 = LocalDate.parse("20190912", DateTimeFormatter.BASIC_ISO_DATE); LocalDate localDate2 = LocalDate.parse("2019-09-12", DateTimeFormatter.ISO_LOCAL_DATE);

1|4ܽ


SimpleDateFormat ȣDateTimeFormatter ̰߳ȫġ

Instant ľȷȸߣԾȷ뼶

Duration ԱݵõʱڵСʱȡ

LocalDateTime ܹٵػȡꡢ¡աһµȡ

TemporalAdjusters аೣõľ̬Լдࡣ


__EOF__

Javaѧϰ   Javaѧ http://www.javalearns.com

ע΢źţjavalearns   ʱѧJava

ɨһɨ

ʱѧJava

Tags:Ϊʲô ʲô  ʹ  µ ʱ  
ߣꡡԴ
    Ǵ֪վ?
  • ѽܵ
  • ٶ
  • Google
  • ַ˽
  • ̫æ˲ǵ
0
ҵ
  • ݣ
  • javaѧϰ(www.javalearns.com) © 2014 Ȩ All Rights Reserved.
  • Email:javalearns@163.com վQQ:1356121699 ICP14003680-3
  • javaѧϰѷַ棬뷢ʼjavalearns@126.comǾ촦
  • Javaѧϰ
  • վͳ
  • 14042902000001