We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Date - a JDK instant Calendar - a JDK calendar String - in ISO8601 format Long - in milliseconds
// java.util java.util.Date java.util.Calendar abstract java.text.DateFormat java.text.SimpleDateFormat(内部持有 calendar, 非线程安全)
java.sql java.sql.TimeStamp
// java.time java.time LocalDateTime LocalDate LocalTime Instant Duration Period java.time.format.DateTimeFormatter DateTimeFormatter.ofPattern("yyyy-MM-dd") java.time.ZoneId
//joda.time 使用最多的五个日期时间类: - Instant - 不可变的类,用来表示时间轴上一个瞬时的点(时间戳) - **DateTime** - 不可变的类,用来替换JDK的Calendar类 - LocalDate - 不可变的类,表示一个本地的日期,而不包含时间部分(没有时区信息) - LocalTime - 不可变的类,表示一个本地的时间,而不包含日期部分(没有时区信息) - LocalDateTime - 不可变的类,表示一个本地的日期-时间(没有时区信息) DateTime(long instant) DateTime(Object instant) DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); DateTime d3 = DateTime.parse("2016-10-10 11:12:55", formatter); DateTime dt = new DateTime(new Date()); jdk的date转换为DateTime Date jdkDate = dt.toDate() 转换为jdk的date LocalDateTime demoDateTime = Instant.ofEpochMilli(DateTime.now().getMillis()) .atZone(ZoneId.systemDefault()) .toLocalDateTime(); demoDateTime.toLocalDate(); demoDateTime.toLocalTime();
对日期的计算方式繁琐,而且容易出错,因为月份是从0开始的,从Calendar中获取的月份需要加一才能表示当前月份.
java 8中的日期/时间类都是不可变的,这是为了保证线程安全。
//将java.util.Date 转换为java8 的java.time.LocalDateTime,默认时区为东8区 public static LocalDateTime dateConvertToLocalDateTime(Date date) { return date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime(); } //将java8 的 java.time.LocalDateTime 转换为 java.util.Date,默认时区为东8区 public static Date localDateTimeConvertToDate(LocalDateTime localDateTime) { return Date.from(localDateTime.toInstant(ZoneOffset.of("+8"))); }
LocalDateTime dateTime = LocalDateTime.now(); String strDate1 = dateTime.format(DateTimeFormatter.BASIC_ISO_DATE); // 20170105 String strDate2 = dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE); // 2017-01-05 String strDate3 = dateTime.format(DateTimeFormatter.ISO_LOCAL_TIME); // 14:20:16.998 String strDate4 = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // 2017-01-05 String strDate5 = dateTime.format(DateTimeFormatter.ofPattern("今天是:YYYY年 MMMM DD日 E", Locale.CHINESE)); // 今天是:2017年 一月 05日 星期四
//java.time.ZoneId, java.util.TimeZone ZoneId shanghaiZoneId = ZoneId.of("Asia/Shanghai"); ZoneId systemZoneId = ZoneId.systemDefault(); Set<String> zoneIds = ZoneId.getAvailableZoneIds(); ZoneId oldToNewZoneId = TimeZone.getDefault().toZoneId(); LocalDateTime localDateTime = LocalDateTime.now(); ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, shanghaiZoneId); // 2017-01-05T15:26:56.147+08:00[Asia/Shanghai] ZoneOffset zoneOffset = ZoneOffset.of("+09:00"); LocalDateTime localDateTime = LocalDateTime.now(); OffsetDateTime offsetDateTime = OffsetDateTime.of(localDateTime, zoneOffset);
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Date - a JDK instant
Calendar - a JDK calendar
String - in ISO8601 format
Long - in milliseconds
java8 LocalDateTime
对日期的计算方式繁琐,而且容易出错,因为月份是从0开始的,从Calendar中获取的月份需要加一才能表示当前月份.
java 8中的日期/时间类都是不可变的,这是为了保证线程安全。
The text was updated successfully, but these errors were encountered: