import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAdjusters;
import java.util.Locale;
import java.util.regex.Pattern;
/**
* description: 日期工具类,
* date: 2018-08-24
*/
public final class DateTool {
// 默认日期时间格式
private static final DateTimeFormatter DEFAULT_DATETIME_FORMATTER = TimeFormatEnum.LONG_DATE_PATTERN_LINE.getFormatter();
// 默认时分秒格式
private static final DateTimeFormatter DEFAULT_TIME_FORMATTER = TimeFormatEnum.TIME_PATTERN_COLON.getFormatter();
// 无参数的构造函数,防止被实例化
private DateTool() {};
// 把毫秒数转换为标准日期时间字符串,格式: yyyy-MM-dd HH:mm:ss
public static String formatMilliSecond(long milliSeconds) {
ZoneId z = ZoneId.systemDefault();
Instant instant = Instant.now();
LocalDateTime datetime = LocalDateTime.ofEpochSecond(milliSeconds / 1000, 0, z.getRules().getOffset(instant));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return datetime.format(formatter);
}
// 把毫秒数转换为标准日期时间字符串,默认格式: yyyy-MM-dd HH:mm:ss
public static String formatMilliSecond(long milliSeconds, DateTimeFormatter formatter) {
ZoneId z = ZoneId.systemDefault();
Instant instant = Instant.now();
LocalDateTime datetime = LocalDateTime.ofEpochSecond(milliSeconds / 1000, 0, z.getRules().getOffset(instant));
formatter = formatter == null ? DEFAULT_DATETIME_FORMATTER : formatter;
return datetime.format(formatter);
}
// 获取当前日期时间字符串,格式: yyyy-MM-dd HH:mm:ss
public static String getNowDateAndTimeString() {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return now.format(formatter);
}
// 获取当前日期时间字符串,默认格式: yyyy-MM-dd HH:mm:ss
public static String getNowDateAndTimeString(DateTimeFormatter formatter) {
LocalDateTime now = LocalDateTime.now();
formatter = formatter == null ? DEFAULT_DATETIME_FORMATTER : formatter;
return now.format(formatter);
}
// 获取当前日期字符串,格式: yyyy-MM-dd
public static String getNowDateString() {
LocalDate today = LocalDate.now();
return today.toString();
}
// 获取当前时间字符串,格式: HH:mm:ss
public static String getNowTimeString() {
LocalTime now = LocalTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
return now.format(formatter);
}
// 获取当前时间字符串,默认格式: HH:mm:ss
public static String getNowTimeString(DateTimeFormatter formatter) {
LocalTime now = LocalTime.now();
formatter = formatter == null ? DEFAULT_TIME_FORMATTER : formatter;
return now.format(formatter);
}
// 获取某年某月第一天
public static String getYearMonthFirstDayString(int year, int month) throws Exception {
if (month < 1 || month > 12) {
throw new Exception("invalid parameters");
}
Integer iyear = Integer.valueOf(year);
Integer imonth = Integer.valueOf(month);
if (month < 10) {
return iyear.toString() + "-0" + imonth.toString() + "-01 00:00:00";
} else {
return iyear.toString() + "-" + imonth.toString() + "-01 00:00:00";
}
}
// 获取某年某月最后一天
public static String getYearMonthLastDayString(int year, int month) throws Exception {
if (month < 1 || month > 12) {
throw new Exception("invalid parameters");
}
LocalDate date = LocalDate.of(year, month, 1);
Integer lastday = date.getMonth().length(date.isLeapYear());
Integer iyear = Integer.valueOf(year);
Integer imonth = Integer.valueOf(month);
if (month < 10) {
return iyear.toString() + "-0" + imonth.toString() + "-" + lastday.toString() + " 23:59:59";
} else {
return iyear.toString() + "-" + imonth.toString() + "-" + lastday.toString() + " 23:59:59";
}
}
// 获取某年某月某日所在周的某一天
public static String getWeekDayString(int year, int month, int day, DayOfWeek dow) throws Exception {
try {
LocalDate date = LocalDate.of(year, month, day);
LocalDate newDate = date.with(TemporalAdjusters.nextOrSame(dow));
return newDate.toString();
} catch (Exception e) {
throw new Exception("invalid parameters");
}
}
// 判断今天是本周第几天
public static int getTodayOfWeek() {
return LocalDate.now().get(ChronoField.DAY_OF_WEEK);
}
// 判断今天是今年第几周
public static int getWeekOfYear() {
return LocalDate.now().get(ChronoField.ALIGNED_WEEK_OF_YEAR);
}
// 判断今天是今年第几周第几天
public static String getDayAndWeek() {
StringBuilder stringBuilder = new StringBuilder();
LocalDate date = LocalDate.now();
stringBuilder.append(date.get(ChronoField.ALIGNED_WEEK_OF_YEAR)).append("周");
stringBuilder.append(date.get(ChronoField.DAY_OF_WEEK)).append("天");
return stringBuilder.toString();
}
// 判断今天是本月第几天
public static int getTodayOfMonth() {
return LocalDate.now().get(ChronoField.DAY_OF_MONTH);
}
// 判断今天是本年第几天
public static int getTodayOfYear() {
return LocalDate.now().get(ChronoField.DAY_OF_YEAR);
}
/**
* 把毫秒数转换为英文格式时间字符串
* 例如: ("dd MMMM yyyy"- 27 August 2018),("dd MMM yyyy" - 27 Aug 2018),("MMM dd yyyy HH:mm:ss a" - Aug 27 2018 20:41:27 PM),
* ("EEE MMM dd HH:mm:ss 'CST' yyyy" - Mon Aug 27 20:42:04 CST 2018),("EEEE MMMM dd HH:mm:ss yyyy" - Monday August 27 20:42:35 2018);
*/
public static String getNowOfEnglish(String patten) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(patten, Locale.ENGLISH);
LocalDateTime now = LocalDateTime.now();
return now.format(formatter);
}
public static void main(String [] args){
System.out.println(getNowOfEnglish("dd"));
}