Skip to content
New issue

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

java date 整理总结 #18

Open
ichengzi opened this issue Jun 4, 2020 · 0 comments
Open

java date 整理总结 #18

ichengzi opened this issue Jun 4, 2020 · 0 comments

Comments

@ichengzi
Copy link
Owner

ichengzi commented Jun 4, 2020

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();

java8 LocalDateTime

对日期的计算方式繁琐,而且容易出错,因为月份是从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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant