建议大家使用Java 8 的日期、时间,而非java.util.Date。
详细原因见:如何在Java 8中愉快地处理日期和时间
总结一下就是,
java.util.Date 太乱,如 月份从0开始、日期和时间的取舍只有自己知道等等。
而java.time.LocalDate则只负责日期,java.time.LocalTime则只负责时间,如果需要日期和时间,还可以使用java.time.LocalDateTime!
以下是几个测试:
1 package start; 2 3 import java.time.LocalDate; 4 import java.time.LocalDateTime; 5 import java.time.LocalTime; 6 import java.time.format.DateTimeFormatter; 7 import java.time.temporal.TemporalAdjusters; 8 import java.util.Locale; 9 10 import org.junit.Test; 11 12 public class Start1 { 13 // LocalDate只负责日期 -- 而java.util.Date虽然包裹各种信息,但别人反而不知你实际的取舍。 14 @Test 15 public void r1() { 16 LocalDate now = LocalDate.now(); 17 System.out.println(now); // 2017-02-22 18 19 LocalDate min = LocalDate.MIN; 20 LocalDate max = LocalDate.MAX; 21 22 System.out.println(min);// -999999999-01-01 23 System.out.println(max);// +999999999-12-31 24 } 25 26 // LocalTime只负责时间,不负责日期。 27 @Test 28 public void r2() { 29 LocalTime noon = LocalTime.NOON; 30 System.out.println(noon); // 12:00 31 System.out.println(LocalTime.MIDNIGHT); // 00:00 32 System.out.println(LocalTime.now());// 15:53:55.036 33 34 } 35 36 // LocalDate获取年月日 37 @Test 38 public void r3() { 39 LocalDate r = LocalDate.of(2012, 11, 27); 40 System.out.println(r); 41 System.out.println(r.getYear());// 2012 42 System.out.println(r.getMonthValue());// 11 43 System.out.println(r.getMonth());// NOVEMBER 44 System.out.println(r.getDayOfYear());// 332 45 System.out.println(r.getDayOfMonth());// 27 46 System.out.println(r.getDayOfWeek());// TUESDAY 47 } 48 49 // LocalDate获取年月的最后一天/第一天 50 @Test 51 public void r31() { 52 LocalDate now = LocalDate.now(); 53 LocalDate withDayOfMonth = now.withDayOfMonth(1);// 修改月的天 54 System.out.println(withDayOfMonth); 55 LocalDate firstday = now.with(TemporalAdjusters.firstDayOfMonth()); 56 System.out.println(firstday); 57 LocalDate lastday = now.with(TemporalAdjusters.lastDayOfMonth()); 58 System.out.println(lastday); 59 60 LocalDate lastdayofyear = now.with(TemporalAdjusters.lastDayOfYear()); 61 System.out.println(lastdayofyear); 62 } 63 64 // LocalDate解析字符串 65 @Test 66 public void r4() { 67 LocalDate date = LocalDate.parse("2017-11-27"); 68 System.out.println(date);// 2017-11-27 69 70 // ##### 71 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu/MM/dd"); 72 LocalDate date2 = LocalDate.parse("2017/11/27", formatter); 73 System.out.println(date2); 74 75 // ###### 76 // DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("uuuu/MM/dd", Locale.CHINESE); 77 DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("MM/dd/uuuu", Locale.CANADA_FRENCH); 78 LocalDate date3 = LocalDate.parse("11/27/2017", formatter3); 79 System.out.println(date3); 80 } 81 82 @Test 83 public void r5() { 84 LocalTime now = LocalTime.now(); 85 System.out.println(now); 86 LocalTime r = LocalTime.of(22, 18); 87 // LocalTime r = LocalTime.of(22, 18, 57); 88 System.out.println(r.getHour());// 22 89 System.out.println(r.getMinute());// 18 90 System.out.println(r.getSecond());// 0 91 System.out.println(r.getNano());// 0 92 } 93 94 @Test 95 public void r6() { 96 LocalTime time = LocalTime.parse("22:18:59"); 97 System.out.println(time); 98 99 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm/ss"); 100 LocalTime t = LocalTime.parse("19:30/31", formatter); 101 System.out.println(t); 102 103 // time.plus() 104 // time.minus() 105 } 106 107 @Test 108 public void r7() { 109 LocalDateTime now = LocalDateTime.now(); 110 System.out.println(now); 111 String str = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE)); 112 System.out.println(str); 113 114 // now.plus() 115 // now.minus() 116 LocalDateTime withHour = now.withHour(22); // 修改了Hour 117 System.out.println(withHour); 118 LocalDateTime withMonth = now.withMonth(8); 119 System.out.println(withMonth); // 修改了Month 120 } 121 122 }
引用:
JDBC 最新JDBC映射将把数据库的日期类型和Java 8的新类型关联起来: SQL -> Java -------------------------- date -> LocalDate time -> LocalTime timestamp -> LocalDateTime 再也不会出现映射到java.util.Date其中日期或时间某些部分为0的情况了。
另外,关于如何正确的存储日期和时间,建议使用绝对时间,即 System.currentTimeMillis() 得到的值,存为BigInt或者Real类型即可。
这样就不再需要考虑时区转换等问题!
详见:如何正确地处理时间