日期时间类
JDK 8 之前的日期时间类
System
类中currentTimeMillis()
java.util.Date
和子类java.sql.Date
SimpleDateFormat
(线程不安全)
Calendar
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
| public class DateTimeTest { @Test public void test1(){ long time = System.currentTimeMillis(); System.out.println(time); }
@Test public void test2(){ Date date1 = new Date(); System.out.println(date1.toString());
System.out.println(date1.getTime());
Date date2 = new Date(155030620410L); System.out.println(date2.toString());
java.sql.Date date3 = new java.sql.Date(35235325345L); System.out.println(date3);
Date date6 = new Date(); java.sql.Date date7 = new java.sql.Date(date6.getTime()); } }
|
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
| import org.junit.Test;
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date;
public class DateTimeTest {
@Test public void testSimpleDateFormat() throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat();
Date date = new Date(); System.out.println(date);
String format = sdf.format(date); System.out.println(format);
String str = "19-12-18 上午11:43"; Date date1 = sdf.parse(str); System.out.println(date1);
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String format1 = sdf1.format(date); System.out.println(format1); Date date2 = sdf1.parse("2020-02-18 11:48:27"); System.out.println(date2); }
@Test public void testCalendar(){ Calendar calendar = Calendar.getInstance();
int days = calendar.get(Calendar.DAY_OF_MONTH); System.out.println(days); System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
calendar.set(Calendar.DAY_OF_MONTH,22); days = calendar.get(Calendar.DAY_OF_MONTH); System.out.println(days);
calendar.add(Calendar.DAY_OF_MONTH,-3); days = calendar.get(Calendar.DAY_OF_MONTH); System.out.println(days);
Date date = calendar.getTime(); System.out.println(date);
Date date1 = new Date(); calendar.setTime(date1); days = calendar.get(Calendar.DAY_OF_MONTH); System.out.println(days); } }
|
JDK 8 中新时间类
LocalDate
LocalTime
LocalDateTime
(常用)
DateTimeFormatter
(类似SimpleTimeFormat
,线程安全的)
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 132 133 134 135 136 137 138 139
| import org.junit.Test;
import java.time.*; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.time.temporal.TemporalAccessor; import java.util.Date;
public class JDK8DateTimeTest {
@Test public void testDate(){ Date date1 = new Date(2020 - 1900,9 - 1,8); System.out.println(date1); }
@Test public void test1(){ LocalDate localDate = LocalDate.now(); LocalTime localTime = LocalTime.now(); LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate); System.out.println(localTime); System.out.println(localDateTime);
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 6, 13, 23, 43); System.out.println(localDateTime1);
System.out.println(localDateTime.getDayOfMonth()); System.out.println(localDateTime.getDayOfWeek()); System.out.println(localDateTime.getMonth()); System.out.println(localDateTime.getMonthValue()); System.out.println(localDateTime.getMinute());
LocalDate localDate1 = localDate.withDayOfMonth(22); System.out.println(localDate); System.out.println(localDate1);
LocalDateTime localDateTime2 = localDateTime.withHour(4); System.out.println(localDateTime); System.out.println(localDateTime2);
LocalDateTime localDateTime3 = localDateTime.plusMonths(3); System.out.println(localDateTime); System.out.println(localDateTime3);
LocalDateTime localDateTime4 = localDateTime.minusDays(6); System.out.println(localDateTime); System.out.println(localDateTime4); }
@Test public void test2(){ Instant instant = Instant.now(); System.out.println(instant);
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8)); System.out.println(offsetDateTime);
long milli = instant.toEpochMilli(); System.out.println(milli);
Instant instant1 = Instant.ofEpochMilli(1550475314878L); System.out.println(instant1); }
@Test public void test3(){
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; LocalDateTime localDateTime = LocalDateTime.now(); String str1 = formatter.format(localDateTime); System.out.println(localDateTime); System.out.println(str1);
TemporalAccessor parse = formatter.parse("2019-02-18T15:42:18.797"); System.out.println(parse);
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG); String str2 = formatter1.format(localDateTime); System.out.println(str2);
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM); String str3 = formatter2.format(LocalDate.now()); System.out.println(str3);
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"); String str4 = formatter3.format(LocalDateTime.now()); System.out.println(str4);
TemporalAccessor accessor = formatter3.parse("2019-02-18 03:52:09"); System.out.println(accessor);
} }
|
比较器
- 自然排序:
Comparable
接口,让类本身修改代码以实现排序
- 定制排序:
Comparator
接口
Comparable
像String、包装类等实现了Comparable
接口,重写了compareTo(obj)
方法,给出了比较两个对象大小的方式。重写compareTo(obj)
的规则:
- 如果当前对象this大于形参对象obj,则返回正整数
- 如果当前对象this小于形参对象obj,则返回负整数
- 如果当前对象this等于形参对象obj,则返回零。
对于自定义类来说,如果需要排序,我们可以让自定义类实现Comparable
接口,重写compareTo(obj)
方法。在compareTo(obj)
方法中指明如何排序。
1 2 3 4 5 6 7
| @Override public int compareTo(Object o) { if(o instanceof xxx){ } throw new RuntimeException("传入的数据类型不一致!"); }
|
Comparator
当元素的类型没有实现Comparable
接口而又不方便修改代码,或者实现了Comparable
接口的排序规则不适合当前的操作,那么可以考虑使用 Comparator
的对象来排序。重写 Comparator
对象的compare(Object o1, Object o2
)方法,比较o1和o2的大小:
- 如果方法返回正整数,则表示o1大于o2;
- 如果返回0,表示相等;
- 返回负整数,表示o1小于o2。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @Test public void test(){ String[] arr = new String[]{"AA","CC","KK","MM","GG","JJ","DD"}; Arrays.sort(arr, new Comparator(){ @Override public int compare(Object o1, Object o2) { if(o1 instanceof String && o2 instanceof String){ String s1 = (String) o1; String s2 = (String) o2; return -s1.compareTo(s2); } throw new RuntimeException("输入的数据类型不一致"); } }); System.out.println(Arrays.toString(arr)); }
|