/** * 日期加法 * * @param txDate 日期格式"20150404" * @param proi DAYS|加上几天|MONTHS|加上几月|YEARS|加上几年 * @param type 选择 DAYS||MONTHS||YEARS * @return */ public static String plus(String txDate, int proi, String type) { if (txDate != null) { DateTime txDT = DateTime.parse(txDate, ISODateTimeFormat.basicDate()); if (type.toUpperCase().endsWith("DAYS")) { return txDT.plusDays(proi).toString(ISODateTimeFormat.basicDate()); } else if (type.toUpperCase().endsWith("MONTHS")) { return txDT.plusMonths(proi).toString(ISODateTimeFormat.basicDate()); } else if (type.toUpperCase().endsWith("YEARS")) { return txDT.plusYears(proi).toString(ISODateTimeFormat.basicDate()); } } return txDate; }
/** * 求两个直接的日期差,月份. * * <p>*MSECONDS|*SECONDS|*MINUTES|*HOURS|*DAYS|*MONTHS|*YEARS * * @param tDate 日期格式"20150404" * @param txDate 日期格式"20150404" * @param type type 选择 DAYS||MONTHS||YEARS * @return */ public static int diff(String tDate, String txDate, String type) { if (txDate != null && tDate != null) { DateTime txDT = DateTime.parse(txDate, ISODateTimeFormat.basicDate()); DateTime tDT = DateTime.parse(tDate, ISODateTimeFormat.basicDate()); if (type.toUpperCase().endsWith("DAYS")) { return Days.daysBetween(txDT, tDT).getDays(); } else if (type.toUpperCase().endsWith("MONTHS")) { return Months.monthsBetween(txDT, tDT).getMonths(); } else if (type.toUpperCase().endsWith("YEARS")) { return Years.yearsBetween(txDT, tDT).getYears(); } else if (type.toUpperCase().endsWith("YEARS")) { return Years.yearsBetween(txDT, tDT).getYears(); } } return 0; }
/** * @param when The instant * @param locale The required locale * @return The instant formatted as "yyyyMMdd" in the system timezone */ public static String formatCompactDateLocal(ReadableInstant when, Locale locale) { if (when == null) { return ""; } return ISODateTimeFormat.basicDate() .withZone(DateTimeZone.getDefault()) .withLocale(locale) .print(when); }
/** * @param when The instant * @return The instant formatted as "yyyyMMdd" in UTC */ public static String formatCompactDate(ReadableInstant when) { if (when == null) { return ""; } return ISODateTimeFormat.basicDate().withZoneUTC().print(when); }
/** * date是 星期几 * * @param date * @return */ public static int week(String date) { DateTime dateTime = DateTime.parse(date, ISODateTimeFormat.basicDate()); return dateTime.dayOfWeek().get(); }