LocalDate類
LocalDate is an immutable datetime class representing a date without a time zone.(LocalDate是一個不可變的datetime類代表一個日期沒有一個時區)
LocalDate is thread-safe and immutable, provided that the Chronology is as well. All standard Chronology classes supplied are thread-safe and immutable. (LocalDate是執行緒安全的和不可改變的,提供的年表。提供所有標準年表類是執行緒安全的和不可改變的。)
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;
}
複製程式碼
LocalDate轉化為int
public static int LocaLDateToInt(LocalDate date) {
return date.getYear() * 10000 + date.getMonthOfYear() * 100 + date.getDayOfMonth();
}
複製程式碼
LocalDate類
public final class LocalDate
extends BaseLocal
implements ReadablePartial, Serializable {
複製程式碼
public static DateTime current() {
return DateTime.now(DateTimeZone.UTC);
}
//2019-04-12T07:10:14.703Z
複製程式碼
DateWork.getAsiaShanghaiTimeZone()
public static DateTimeZone getAsiaShanghaiTimeZone() {
return DateTimeZone.forID("Asia/Shanghai");
}
@FromString
public static DateTimeZone forID(String id) {
if (id == null) {
return getDefault();
}
if (id.equals("UTC")) {
return DateTimeZone.UTC;
}
DateTimeZone zone = getProvider().getZone(id);
if (zone != null) {
return zone;
}
if (id.startsWith("+") || id.startsWith("-")) {
int offset = parseOffset(id);
if (offset == 0L) {
return DateTimeZone.UTC;
} else {
id = printOffset(offset);
return fixedOffsetZone(id, offset);
}
}
throw new IllegalArgumentException("The datetime zone id '" + id + "' is not recognised");
}
//Asia/Shanghai
複製程式碼
System.out.println(DateUtils.formatToString(DateUtils.current().toDate(), "yyyyMMddHHmm"));
public static String formatToString(Date date, String format) {
if (date == null) {
return "";
} else {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
}
//201904121525
複製程式碼
public static String formatDate(ReadableInstant instant, String pattern) {
return DateTimeFormat.forPattern(pattern).print(instant);
}
複製程式碼
DateWorks.formatToDate(operateTime, "yyyy-MM-dd HH:mm:ss")
public static Date formatToDate(String dateString, String format) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.parse(dateString);
}
複製程式碼
//返回時區(Asia/Shanghai)
TimeZone timeZone =DateUtils.getAsiaShanghaiTimeZone().toTimeZone();
//
public static int getYear(Date date, TimeZone timeZone) {
Calendar c = Calendar.getInstance(timeZone);
c.setTime(date);
int result = c.get(Calendar.YEAR);
return result;
}
System.out.println(DateWorks.getYear(DateUtils.current().toDate(), timeZone));
//2019
複製程式碼
DateTime中LocalDate()方法日期型別:
/**
* Converts this object to a <code>LocalDate</code> with the
* same date and chronology.
*
* @return a LocalDate with the same date and chronology
* @since 1.3
*/
public LocalDate toLocalDate() {
return new LocalDate(getMillis(), getChronology());
}
測試:DateTime.now(DateTimeZone.UTC).toLocalDate();
結果:2019-04-12
複製程式碼
DateTime中toLocalDateTime()方法日期型別:
/**
* Converts this object to a <code>LocalDateTime</code> with
* the same datetime and chronology.
*
* @return a LocalDateTime with the same datetime and chronology
* @since 1.3
*/
public LocalDateTime toLocalDateTime() {
return new LocalDateTime(getMillis(), getChronology());
}
測試:DateTime.now(DateTimeZone.UTC).toLocalDateTime()
結果:2019-04-12T08:25:34.576
複製程式碼
MutableDateTime類
解釋: MutableDateTime is mutable and not thread-safe, unless concurrent threads(MutableDateTime是可變的,而不是執行緒安全的,除非併發執行緒不呼叫mutator方法。)
public class MutableDateTime
extends BaseDateTime
implements ReadWritableDateTime, Cloneable, Serializable {
複製程式碼
示例
public static LocalDateRangeClass 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 LocalDateRangeClass(DateUtils.LocaLDateToInt(from), DateUtils.LocaLDateToInt(to));
}
複製程式碼