Java 8 time and date

Source: Internet
Author: User
Tags time and date



La La la


package lime.java1_8.time;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
* *
* @Author liangmy
* @Date 2018/7/16
* /
public class Localdatetime {
public static void main(String[] args) {
//Take the current date:
LocalDate today = LocalDate.now();
System.out.println(today);
//According to the date, December is 12:
LocalDate crischristmas = LocalDate.of(2018, 07, 16);
System.out.println(crischristmas);
//Based on string:
LocalDate endOfFeb = LocalDate.parse("2018-07-16");
System.out.println(endOfFeb);
//Take the first day of this month:
LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth());
System.out.println(firstDayOfThisMonth);
//Take the 2nd day of this month:
LocalDate secondDayOfThisMonth = today.withDayOfMonth(2);
System.out.println(secondDayOfThisMonth);
//Take the last day of the month, no longer need to calculate whether it is 28, 29, 30 or 31:
LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth());
System.out.println(lastDayOfThisMonth);
//Next day:
LocalDate firstDayOf2018 = lastDayOfThisMonth.plusDays(1);
System.out.println(firstDayOf2018);
//On the first Monday of January 2018, a lot of brain cells will die in the calendar:
LocalDate firstMondayOf2018 = today.withMonth(1).with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
System.out.println(firstMondayOf2018);
//Localtime contains milliseconds:
LocalTime now = LocalTime.now();
System.out.println(now);
//Clear milliseconds:
LocalTime nowWithNano0 = LocalTime.now().withNano(0);
System.out.println(nowWithNano0);
//Construction time:
LocalTime zeroHM = LocalTime.of(0,0);
System.out.println(zeroHM);
LocalTime zeroHMS = LocalTime.of(0, 0, 0);
System.out.println(zeroHMS);
LocalTime zeroHMSN = LocalTime.of(0, 0, 0, 0);
System.out.println(zeroHMSN);
LocalTime midHMStr = LocalTime.parse("12:00");
System.out.println(midHMStr);
LocalTime midHMSStr = LocalTime.parse("12:00:00");
System.out.println(midHMSStr);
//Create an instance of locatdatetime
LocalDateTime localDateTimeNow = LocalDateTime.now();
System.out.println(localDateTimeNow);
LocalDateTime localDateTimeOf = LocalDateTime.of(2018,1, 1, 00, 00, 00, 12);
System.out.println(localDateTimeOf);
//Instant: time stamp
//UTC time zone is used by default
Instant insNow = Instant.now(); 
System.out.println(insNow);
OffsetDateTime insUTC8 = insNow.atOffset(ZoneOffset.ofHours(8));
System.out.println(insUTC8);
System.out.println(insNow.getNano());
//Get UTC time zone timestamp of 5 seconds after midnight on January 1, 1970
Instant ofEpochSecond = Instant.ofEpochSecond(5);
System.out.println(ofEpochSecond);
//Duration: used to calculate two "time" intervals
//Period: used to calculate two "date" intervals
LocalTime startTime = LocalTime.now();
LocalTime endTime = startTime.plusHours(1).plusMinutes(2).plusSeconds(3).plusNanos(4);
Duration duration = Duration.between(startTime, endTime);
System.out.println(duration);
System.out.println(duration.getUnits());
System.out.println(duration.getSeconds());
System.out.println(duration.getNano());
LocalDate startDay = LocalDate.now();
LocalDate endDay = startDay.plusYears(1).plusMonths(2).plusDays(3).plusWeeks(4);
Period period = Period.between(startDay, endDay);
System.out.println(period);
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
System.out.println(period.getUnits());
System.out.println(period.getChronology());
//Temporaladjuster: time corrector
LocalDate with = today.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println(with);
//DateTimeFormatter
//Predefined standard formats
//Locale related formats
//Custom format
Datetimeformatter datetimeformatter = datetimeformatter.ofpattern ("yyyy MM DD HH: mm: SS e");
String localDateTime = LocalDateTime.now().format(dateTimeFormatter);
System.out.println(localDateTime);
//Time zone processing
//ZonedDate、 ZonedTime、 ZonedDateTime
//Each time zone corresponds to the ID, and the region ID is in the format of "{region} / {City}" Asia / Shanghai
//Zoneid: this class contains all time zone information
//Getavailablezones(): all time zone and time zone information can be obtained
//Of (ID): get zoneid object with specified time zone information
LocalDateTime zoneAsiaShanghai = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
System.out.println(zoneAsiaShanghai);
LocalDateTime zoneSystemDefault = LocalDateTime.now(ZoneId.systemDefault());
System.out.println(zoneSystemDefault);
//from(Instant instant)
//toInstant()
// Long - Instant
Instant instant = Instant.ofEpochMilli(System.currentTimeMillis());
System.out.println(instant);
//Instant - Long
Long timestamp = Instant.now().toEpochMilli();
System.out.println(timestamp);
// Date - LocalDateTime
LocalDateTime date2LocalDateTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
System.out.println(date2LocalDateTime);
// Date - LocalDate
LocalDate date2LocalDate = date2LocalDateTime.toLocalDate();
System.out.println(date2LocalDate);
// Date - LocalTime
LocalTime date2LocalTime = date2LocalDateTime.toLocalTime();
System.out.println(date2LocalTime);
// LocalDateTime - Date
Date localDateTime2Date = Date.from(date2LocalDateTime.toInstant(ZoneOffset.ofHours(8)));
System.out.println(localDateTime2Date);
// LocalDate - Date
Date localDate2Date = Date.from(date2LocalDate.atStartOfDay().toInstant(ZoneOffset.ofHours(8)));
System.out.println(localDate2Date);
// LocalTime - Date
Date localTime2Date = Date.from(LocalDateTime.of(LocalDate.now(), date2LocalTime).toInstant(ZoneOffset.ofHours(8)));
System.out.println(localTime2Date);
}
} 


La La la



Java 8 time and date


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.