转载

java时间处理

LocalDate is an immutable datetime class representing a date without a time zone.

//LocalDate是一个不可变的datetime类代表一个日期没有一个时区。

public static DateTimeZone getDefault() {
        DateTimeZone zone = cDefault.get();
        if (zone == null) {
            try {
                try {
                    String id = System.getProperty("user.timezone");
                    if (id != null) {  // null check avoids stack overflow
                        zone = forID(id);
                    }
                } catch (RuntimeException ex) {
                    // ignored
                }
                if (zone == null) {
                    zone = forTimeZone(TimeZone.getDefault());
                }
            } catch (IllegalArgumentException ex) {
                // ignored
            }
            if (zone == null) {
                zone = UTC;
            }
            if (!cDefault.compareAndSet(null, zone)) {
                zone = cDefault.get();
            }
        }
        return zone;
    }

复制代码

时间示例:

public static LocalDateRange getThisMonth(TimeZone timeZone) {
		DateTimeZone tz = DateTimeZone.forTimeZone(timeZone);//时区Asia/Shanghai
		MutableDateTime mdt = new MutableDateTime(tz);
		mdt.setDayOfMonth(2);//当月的第一天
		resetDayTime(mdt);//
		LocalDate from = mdt.toDateTime().toLocalDate();//2019-04-01
		mdt.addMonths(1);//增加一月
		mdt.setDayOfMonth(1);增加一月的某一天
		resetDayTime(mdt);
		mdt.addMillis(-1);2019-05-01 改变为2019-04-30
		LocalDate to = mdt.toDateTime().toLocalDate();//2019-04-30
		return new LocalDateRange(DateUtils.LocaLDateToInt(from), DateUtils.LocaLDateToInt(to));
		}
复制代码
public static int LocaLDateToInt(LocalDate date) {
		return date.getYear() * 10000 + date.getMonthOfYear() * 100 + date.getDayOfMonth();
	}
复制代码
public final class LocalDate
        extends BaseLocal
        implements ReadablePartial, Serializable {
复制代码
原文  https://juejin.im/post/5cb027c06fb9a068ad1b0d42
正文到此结束
Loading...