JDK8中新的日期时间类的使用方式
研究了好久jdk8,刚刚看完jdk8新的日期时间类,当然,相较以前还是有很多改进之处的,不多说,直接上代码,都是很简单的测试,相信都能看懂的:
1 package org.test.datetimetest; 2 3 import java.time.Duration; 4 import java.time.Instant; 5 import java.time.LocalDate; 6 import java.time.LocalDateTime; 7 import java.time.LocalTime; 8 import java.time.Month; 9 import java.time.Period; 10 import java.time.ZoneId; 11 import java.time.ZoneOffset; 12 import java.time.ZonedDateTime; 13 import java.time.format.DateTimeFormatter; 14 import java.time.temporal.TemporalAdjusters; 15 import java.util.Calendar; 16 import java.util.Date; 17 import java.util.GregorianCalendar; 18 import java.util.TimeZone; 19 20 public class DateTimeTest { 21 22 public static void main(String[] args) { 23 24 dateTest(); 25 timeTest(); 26 localDateTimeTest(); 27 instantTest(); 28 dateTimeUtils(); 29 dateParseFormatTest(); 30 dateAPILegacySupport(); 31 } 32 33 private static void dateTest() { 34 35 LocalDate today = LocalDate.now(); 36 System.out.println("当前日期:" + today); 37 38 LocalDate firstDay_2014 = LocalDate.of(2014, Month.JANUARY, 1); 39 System.out.println("2014年第一天:" + firstDay_2014); 40 41 LocalDate todayAsia = LocalDate.now(ZoneId.of("Asia/Kolkata")); 42 System.out.println("亚洲时区的当前日期:" + todayAsia); 43 44 LocalDate ofEpochDay = LocalDate.ofEpochDay(365); 45 System.out.println("基准日期点" + ofEpochDay); 46 47 LocalDate ofYearDay = LocalDate.ofYearDay(2014, 100); 48 System.out.println("2014年第100天的日期" + ofYearDay); 49 } 50 51 private static void timeTest() { 52 53 LocalTime now = LocalTime.now(); 54 System.out.println(now); 55 56 LocalTime setTime = LocalTime.of(23, 25, 45, 20); 57 System.out.println(setTime); 58 59 LocalTime timeOfAsia = LocalTime.now(ZoneId.of("Asia/Kolkata")); 60 System.out.println(timeOfAsia); 61 62 LocalTime ofSecondOfDay = LocalTime.ofSecondOfDay(10000); 63 System.out.println(ofSecondOfDay); 64 } 65 66 private static void localDateTimeTest() { 67 68 //Current Date 69 LocalDateTime today = LocalDateTime.now(); 70 System.out.println("当前日期时间 DateTime="+today); 71 //Current Date using LocalDate and LocalTime 72 today = LocalDateTime.of(LocalDate.now(), LocalTime.now()); 73 System.out.println("当前日期时间="+today); 74 //Creating LocalDateTime by providing input arguments 75 LocalDateTime specificDate = LocalDateTime.of(2014, Month.JANUARY, 1, 10, 10, 30); 76 System.out.println("Specific Date="+specificDate); 77 //Try creating date by providing invalid inputs 78 //LocalDateTime feb29_2014 = LocalDateTime.of(2014, Month.FEBRUARY, 28, 25,1,1); 79 //Exception in thread "main" java.time.DateTimeException: 80 //Invalid value for HourOfDay (valid values 0 - 23): 25 81 //Current date in "Asia/Kolkata", you can get it from ZoneId javadoc 82 LocalDateTime todayKolkata = LocalDateTime.now(ZoneId.of("Asia/Kolkata")); 83 System.out.println("Current Date in IST="+todayKolkata); 84 //java.time.zone.ZoneRulesException: Unknown time-zone ID: IST 85 //LocalDateTime todayIST = LocalDateTime.now(ZoneId.of("IST")); 86 //Getting date from the base date i.e 01/01/1970 87 LocalDateTime dateFromBase = LocalDateTime.ofEpochSecond(10000, 0, ZoneOffset.UTC); 88 System.out.println("10000th second time from 01/01/1970= "+dateFromBase); 89 } 90 91 private static void instantTest() { 92 93 Instant instant = Instant.now(); 94 System.out.println(instant); 95 96 Instant ofEpochMilli = Instant.ofEpochMilli(instant.toEpochMilli()); 97 System.out.println(ofEpochMilli); 98 99 Duration duration = Duration.ofDays(30); 100 System.out.println(duration); 101 102 } 103 104 private static void dateTimeUtils() { 105 106 LocalDate today = LocalDate.now(); 107 //Get the Year, check if it's leap year 108 System.out.println("Year "+today.getYear()+" is Leap Year? "+today.isLeapYear()); 109 //Compare two LocalDate for before and after 110 System.out.println("Today is before 01/01/2015? "+today.isBefore(LocalDate.of(2015,1,1))); 111 //Create LocalDateTime from LocalDate 112 System.out.println("Current Time="+today.atTime(LocalTime.now())); 113 //plus and minus operations 114 System.out.println("10 days after today will be "+today.plusDays(10)); 115 System.out.println("3 weeks after today will be "+today.plusWeeks(3)); 116 System.out.println("20 months after today will be "+today.plusMonths(20)); 117 System.out.println("10 days before today will be "+today.minusDays(10)); 118 System.out.println("3 weeks before today will be "+today.minusWeeks(3)); 119 System.out.println("20 months before today will be "+today.minusMonths(20)); 120 //Temporal adjusters for adjusting the dates 121 System.out.println("First date of this month= "+today.with(TemporalAdjusters.firstDayOfMonth())); 122 LocalDate lastDayOfYear = today.with(TemporalAdjusters.lastDayOfYear()); 123 System.out.println("Last date of this year= "+lastDayOfYear); 124 Period period = today.until(lastDayOfYear); 125 System.out.println("Period Format= "+period); 126 System.out.println("Months remaining in the year= "+period.getMonths()); 127 } 128 129 private static void dateParseFormatTest() { 130 131 LocalDate today = LocalDate.now(); 132 System.out.println("today:" + today); 133 System.out.println(today.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"))); 134 System.out.println(today.format(DateTimeFormatter.BASIC_ISO_DATE)); 135 136 LocalDateTime now = LocalDateTime.now(); 137 System.out.println("now:" + now); 138 System.out.println(now.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:SS"))); 139 System.out.println(now.format(DateTimeFormatter.BASIC_ISO_DATE)); 140 141 Instant timestamp = Instant.now(); 142 //default format 143 System.out.println("Default format of Instant="+timestamp); 144 //Parse examples,这部分有问题 145 //LocalDateTime dt = LocalDateTime.parse("27::Apr::2014 21::39::48", 146 //DateTimeFormatter.ofPattern("d::MMM::uuuu HH::mm::ss")); 147 //System.out.println("Default format after parsing = "+dt); 148 } 149 150 private static void dateAPILegacySupport() { 151 152 //Date to Instant 153 Instant timestamp = new Date().toInstant(); 154 //Now we can convert Instant to LocalDateTime or other similar classes 155 LocalDateTime date = LocalDateTime.ofInstant(timestamp, 156 ZoneId.of(ZoneId.SHORT_IDS.get("PST"))); 157 System.out.println("Date = "+date); 158 //Calendar to Instant 159 Instant time = Calendar.getInstance().toInstant(); 160 System.out.println(time); 161 //TimeZone to ZoneId 162 ZoneId defaultZone = TimeZone.getDefault().toZoneId(); 163 System.out.println(defaultZone); 164 //ZonedDateTime from specific Calendar 165 ZonedDateTime gregorianCalendarDateTime = new GregorianCalendar().toZonedDateTime(); 166 System.out.println(gregorianCalendarDateTime); 167 //Date API to Legacy classes 168 Date dt = Date.from(Instant.now()); 169 System.out.println(dt); 170 TimeZone tz = TimeZone.getTimeZone(defaultZone); 171 System.out.println(tz); 172 GregorianCalendar gc = GregorianCalendar.from(gregorianCalendarDateTime); 173 System.out.println(gc); 174 } 175 }
下一次,发布jdk8中stream的使用方式,还有lambda表达式的使用方式,特别是stream,感觉用起来好方便啊!!!