1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| public class TimeIntroduction { public static void testClock() throws InterruptedException { Clock c1 = Clock.systemUTC(); System.out.println(c1.millis()); Clock c2 = Clock.systemDefaultZone(); Clock c31 = Clock.system(ZoneId.of("Europe/Paris")); System.out.println(c31.millis()); Clock c32 = Clock.system(ZoneId.of("Asia/Shanghai")); System.out.println(c32.millis()); Clock c4 = Clock.fixed(Instant.now(), ZoneId.of("Asia/Shanghai")); System.out.println(c4.millis()); Thread.sleep(1000); System.out.println(c4.millis()); Clock c5 = Clock.offset(c1, Duration.ofSeconds(2)); System.out.println(c1.millis()); System.out.println(c5.millis()); } public static void testInstant() { Instant instant1 = Instant.now(); System.out.println(instant1.getEpochSecond()); System.out.println(instant1.toEpochMilli()); Clock clock1 = Clock.systemUTC(); Instant instant2 = Instant.now(clock1); System.out.println(instant2.toEpochMilli()); Clock clock2 = Clock.fixed(instant1, ZoneId.systemDefault()); Instant instant3 = Instant.now(clock2); System.out.println(instant3.toEpochMilli()); String dateTime = "2018-06-07 10:19:00"; DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault()); Instant.from(format.parse(dateTime)).getEpochSecond() } public static void testLocalDateTime() { LocalDateTime now = LocalDateTime.now(); System.out.println(now);
LocalDateTime now2 = LocalDateTime.now(ZoneId.of("Europe/Paris")); System.out.println(now2);
Clock clock = Clock.system(ZoneId.of("Asia/Dhaka")); LocalDateTime now3 = LocalDateTime.now(clock); System.out.println(now3);
LocalDateTime d1 = LocalDateTime.of(2018, 12, 31, 23, 59);
LocalDateTime d2 = LocalDateTime.of(2018, 12, 31, 23, 59, 59, 11);
Instant instant = Instant.now(); LocalDateTime d3 = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()); System.out.println(d3);
LocalDateTime d4 = LocalDateTime.parse("2018-12-31T23:59"); System.out.println(d4); LocalDateTime d5 = LocalDateTime.parse("2018-12-31T23:59:59.999"); System.out.println(d5);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); LocalDateTime d6 = LocalDateTime.parse("2018/12/31 23:59:59", formatter); System.out.println(formatter.format(d6));
System.out.println(d6.getYear()); System.out.println(d6.getMonth()); System.out.println(d6.getDayOfYear()); System.out.println(d6.getDayOfMonth()); System.out.println(d6.getDayOfWeek()); System.out.println(d6.getHour()); System.out.println(d6.getMinute()); System.out.println(d6.getSecond()); System.out.println(d6.getNano());
LocalDateTime d7 = d6.minusDays(1); LocalDateTime d8 = d7.plus(1, IsoFields.QUARTER_YEARS);
} public static void testZonedDateTime() {
ZonedDateTime now = ZonedDateTime.now(); System.out.println(now); ZonedDateTime now2 = ZonedDateTime.now(ZoneId.of("Europe/Paris")); System.out.println(now2);
ZonedDateTime z1 = ZonedDateTime.parse("2018-12-31T23:59:59Z[Europe/Paris]"); System.out.println(z1); } public static void testDuration() { Duration d1 = Duration.between(Instant.ofEpochMilli(System.currentTimeMillis() - 12323123), Instant.now());
System.out.println(d1.toDays()); System.out.println(d1.toHours()); System.out.println(d1.toMinutes()); System.out.println(d1.toMillis()); System.out.println(d1.toNanos());
Duration d2 = Duration.ofDays(1); System.out.println(d2.toDays()); } public static void testChronology() { Chronology c = HijrahChronology.INSTANCE; ChronoLocalDateTime d = c.localDateTime(LocalDateTime.now()); System.out.println(d); }
public static void testNewOldDateConversion(){ Instant instant=new Date().toInstant(); Date date=Date.from(instant); System.out.println(instant); System.out.println(date); } public static void main(String[] args) throws InterruptedException { testClock(); testInstant(); testLocalDateTime(); testZonedDateTime(); testDuration(); testChronology(); testNewOldDateConversion(); } }
|