date和calendar日期的用法
util的data转换成sql的data
java.util.Date utilDate = new java.util.Date();
java.sql.Date sDate = new java.sql.Date(uDate.getTime());
创建Date对象
Date date = new Date();
System.out.println(date);
Calendar calendar = Calendar.getInstance();
calendar.set(90, Calendar.DECEMBER, 11);
Date myBirthDate = calendar.getTime();
System.out.println(myBirthDate);
calendar.set(90, Calendar.DECEMBER, 11, 8, 32, 35);
Date myBirthDatenTime = calendar.getTime();
System.out.println(myBirthDatenTime);
格式化
String formatString = "yyyy/MM/dd hh:mm.ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatString);
Date date = Calendar.getInstance().getTime();
String formattedDate = simpleDateFormat.format(date);
Instant
Instant now = Instant.now();
Instant epoch1 = Instant.ofEpochMilli(0);
Instant epoch2 = Instant.parse("1970-01-01T00:00:00Z");
java.time.temporal.ChronoUnit.MICROS.between(epoch1, epoch2);
ChronoUnit
时间间隔
LocalTime start = LocalTime.of(1, 0, 0); // hour, minute, second
LocalTime end = LocalTime.of(2, 10, 20); // hour, minute, second
long halfDays1 = start.until(end, ChronoUnit.HALF_DAYS); // 0
long halfDays2 = ChronoUnit.HALF_DAYS.between(start, end); // 0
long hours1 = start.until(end, ChronoUnit.HOURS); // 1
long hours2 = ChronoUnit.HOURS.between(start, end); // 1
long minutes1 = start.until(end, ChronoUnit.MINUTES); // 70
long minutes2 = ChronoUnit.MINUTES.between(start, end); // 70
long seconds1 = start.until(end, ChronoUnit.SECONDS); // 4220
long seconds2 = ChronoUnit.SECONDS.between(start, end); // 4220
long millisecs1 = start.until(end, ChronoUnit.MILLIS); // 4220000
long millisecs2 = ChronoUnit.MILLIS.between(start, end); // 4220000
long microsecs1 = start.until(end, ChronoUnit.MICROS); // 4220000000
long microsecs2 = ChronoUnit.MICROS.between(start, end); // 4220000000
long nanosecs1 = start.until(end, ChronoUnit.NANOS); // 4220000000000
long nanosecs2 = ChronoUnit.NANOS.between(start, end); // 4220000000000
LocalTime
只有时
基本用法
LocalTime time = LocalTime.now();
time = LocalTime.MIDNIGHT;
time = LocalTime.NOON;
time = LocalTime.of(12, 12, 45);
time.plusMinutes(1);
time.getMinutes();
time.minusMinutes(1);
转换成Data
LocalTime lTime = LocalTime.now();
Instant instant = lTime.atDate(LocalDate.of(A_YEAR, A_MONTH, A_DAY)).
atZone(ZoneId.systemDefault()).toInstant();
Date time = Date.from(instant);
LocalDate
没有时
基本用法
LocalDate lDate = LocalDate.now();
lDate = LocalDate.of(2017, 12, 15);
lDate = LocalDate.parse("2017-12-15");
LocalDate.now(ZoneId.systemDefault());
LocalData转换成Data
Date date = Date.from(Instant.now());
ZoneId defaultZoneId = ZoneId.systemDefault();
LocalDate localDate = date.toInstant().atZone(defaultZoneId).toLocalDate();
Date.from(localDate.atStartOfDay(defaultZoneId).toInstant());
其他用法
LocalDate tomorrow = LocalDate.now().plusDays(1);
LocalDateTime anHourFromNow = LocalDateTime.now().plusHours(1);
Long daysBetween = java.time.temporal.ChronoUnit.DAYS.between(LocalDate.now(),
LocalDate.now().plusDays(3)); // 3
Duration duration = Duration.between(Instant.now(),
ZonedDateTime.parse("2016-07-27T07:00:00+01:00[Europe/Stockholm]"))
LocalDateTime
基本用法
LocalDateTime lDateTime = LocalDateTime.now();
lDateTime = LocalDateTime.of(2017, 12, 15, 11, 30);
lDateTime = LocalDateTime.parse("2017-12-05T11:30:30");
LocalDateTime.now(ZoneId.systemDefault());
LocalDateTime转换成Data
Date date = Date.from(Instant.now());
ZoneId defaultZoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = date.toInstant().atZone(defaultZoneId).toLocalDateTime();
Date out = Date.from(localDateTime.atZone(defaultZoneId).toInstant());
其他用法
LocalDateTime ldp1 = LocalDateTime.parse("2014-03-25T01:30");
String pattern = "d-MM-yyyy HH:mm";
DateTimeFormatter dtF1 = DateTimeFormatter.ofPattern(pattern);
ldp2 = LocalDateTime.parse("15-05-2016 13:55",dtF1);
System.out.println(ldp1 + "
" + ldp2);
DateTimeFormatter dtF2 = DateTimeFormatter.ofPattern("EEE d, MMMM, yyyy HH:mm");
DateTimeFormatter dtF3 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime ldtf1 = LocalDateTime.now();
System.out.println(ldtf1.format(dtF2) +"
"+ldtf1.format(dtF3))
Date转换成字符串
Date today = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy");
System.out.println(dateFormat.format(today));
dateFormat.applyPattern("dd-MM-yyyy");
System.out.println(dateFormat.format(today));
dateFormat.applyPattern("dd-MM-yyyy HH:mm:ss E");
System.out.println(dateFormat.format(today));
字符串转换成Date
第一种
Date date = null;
SimpleDateFormat objDf = new SimpleDateFormat(dateFormat);
date = objDf.parse(StringDate);
第二种
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
String dateStr = "02/25/2016";
Date date = dateFormat.parse(dateStr);
System.out.println(date.getYear());
SHORT 6/30/09 30/06/09
MEDIUM Jun 30, 2009 30 juin 2009
LONG June 30, 2009 30 juin 2009
FULL Tuesday, June 30, 2009 mardi 30 juin 2009
比较日期大小
Boolean.valueOf(today.before(afterday)));
Boolean.valueOf(today.after(afterday)));
Boolean.valueOf(today.equals(afterday)));
Boolean.valueOf(today.isBefore(afterday)));
Boolean.valueOf(today.isAfter(afterday)));
Integer.valueOf(today.compareTo(afterday)));
Boolean.valueOf(today.isEqual(afterday)));
时区
System.out.println(TimeZone.getDefault().getDisplayName());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
sdf.setTimeZone(TimeZone.getTimeZone("Europe/London"));
System.out.println(sdf.format(date));
Calendar
Calendar calendar = Calendar.getInstance();
Calendar gregorianCalendar = new GregorianCalendar(); // 创建实例
Calendar gregorianCalendarAtSpecificDay = new GregorianCalendar(2016, Calendar.JANUARY, 1);
Calendar gregorianCalendarAtSpecificDayAndTime = new GregorianCalendar(2016, Calendar.JANUARY, 1,6, 55, 10);
calendar.add(Calendar.MONTH, -6); // 增
calendar.roll(Calendar.MONTH, -6); // 减
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
if (cal.get(Calendar.AM_PM) == Calendar.PM) System.out.println("It is PM");