代码语言:javascript复制
/**
* Java8时间方法LocalDate和LocalDateTime
* getYear() int 获取当前日期的年份
* getMonth() Month 获取当前日期的月份对象
* getMonthValue() int 获取当前日期是第几月
* getDayOfWeek() DayOfWeek 表示该对象表示的日期是星期几
* getDayOfMonth() int 表示该对象表示的日期是这个月第几天
* getDayOfYear() int 表示该对象表示的日期是今年第几天
* withYear(int year) LocalDate 修改当前对象的年份
* withMonth(int month) LocalDate 修改当前对象的月份
* withDayOfMonth(int dayOfMonth) LocalDate 修改当前对象在当月的日期
* isLeapYear() boolean 是否是闰年
* lengthOfMonth() int 这个月有多少天
* lengthOfYear() int 该对象表示的年份有多少天(365或者366)
* plusYears(long yearsToAdd) LocalDate 当前对象增加指定的年份数
* plusMonths(long monthsToAdd) LocalDate 当前对象增加指定的月份数
* plusWeeks(long weeksToAdd) LocalDate 当前对象增加指定的周数
* plusDays(long daysToAdd) LocalDate 当前对象增加指定的天数
* minusYears(long yearsToSubtract) LocalDate 当前对象减去指定的年数
* minusMonths(long monthsToSubtract) LocalDate 当前对象减去注定的月数
* minusWeeks(long weeksToSubtract) LocalDate 当前对象减去指定的周数
* minusDays(long daysToSubtract) LocalDate 当前对象减去指定的天数
* compareTo(ChronoLocalDate other) int 比较当前对象和other对象在时间上的大小,返回值如果为正,则当前对象时间较晚,
* isBefore(ChronoLocalDate other) boolean 比较当前对象日期是否在other对象日期之前
* isAfter(ChronoLocalDate other) boolean 比较当前对象日期是否在other对象日期之后
* isEqual(ChronoLocalDate other) boolean 比较两个日期对象是否相等
*/
/**
* @author Chrison
* Created at 2019/11/11
*/
public class DateUtils4Java8 {
public static final String DATE_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_FORMAT_SHORT = "yyyy-MM-dd";
public static final String DATE_FORMAT_SHORT_2 = "MM-dd HH:mm:ss";
public static final String DATE_FORMAT_COMPACT = "yyyyMMdd";
public static final String DATE_FORMAT_COMPACTFULL = "yyyyMMddHHmmss";
public static final String DATE_FORMAT_FULL_MSEL = "yyyyMMddHHmmssSSSS";
public static final String DATE_YEAR_MONTH = "yyyyMM";
public static final String DATE_FORMAT_FULL_MSE = "yyyyMMddHHmmssSSS";
/**
* 获取系统当前日期
*
* @return
*/
public static Date getCurrentDate() {
return new Date();
}
/**
* 获取系统当前日期
*
* @return
*/
public static LocalDateTime getCurrentLocalDateTime() {
return LocalDateTime.now();
}
/**
* 根据时间格式返回对应的String类型的时间
*
* @param format
* @return
*/
public static String getCurDateTime(String format) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
String dataTime = now.format(dateTimeFormatter);
return dataTime;
}
/**
* 得到当前日期
*
* @return String 当前日期 yyyy-MM-dd HH:mm:ss格式
* @author kevin
*/
public static String getCurDateTimeFull() {
return getCurDateTime(DATE_FORMAT_FULL);
}
/**
* 得到当前日期
*
* @return String 当前日期 yyyy-MM-dd HH:mm:ss格式
* @author kevin
*/
public static String getCurDateTimeShort2() {
return getCurDateTime(DATE_FORMAT_SHORT_2);
}
/**
* 得到当前日期
*
* @return String 当前日期 yyyyMMddHHmmss格式
* @author kevin
*/
public static String getCurDateTime1() {
return getCurDateTime(DATE_FORMAT_FULL);
}
/**
* 得到当前日期YYYYMM格式
*
* @return String 当前日期 yyyyMM格式
* @author kevin
*/
public static String getCurDateYYYYMM() {
return getCurDateTime(DATE_YEAR_MONTH);
}
/**
* 得到当前日期
*
* @return String 当前日期 yyyyMMdd格式
* @author kevin
*/
public static String getCurDateYYYYMMDD() {
return getCurDateTime(DATE_FORMAT_COMPACT);
}
/**
* 判断是否是今天
*
* @param strDate
* @return
*/
public static boolean isCurrentDay(String strDate) {
boolean bRet = false;
LocalDate strLocalDate = LocalDate.parse(strDate);
if (LocalDate.now().getYear() == strLocalDate.getYear()) {
MonthDay monthDay = MonthDay.from(strLocalDate);
MonthDay today = MonthDay.from(LocalDate.now());
return monthDay.equals(today);
}
return bRet;
}
/**
* 获取几小时后的时间
*
* @param hour
* @param format
* @return
*/
public static String getAfterDateTime(int hour, String format) {
LocalTime localTime = LocalTime.now().plusHours(hour);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
String dataTime = localTime.format(dateTimeFormatter);
return dataTime;
}
/**
* 当前日期时间戳(yyyyMMddHHmmssSSSS)
*
* @return
* @author liangxuekai
*/
public static String getTimeStamp() {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT_FULL_MSEL);
return now.format(dateTimeFormatter);
}
/**
* 日期转字符串
*
* @return String
* @author kevin
*/
public static String parseDateToString(Date thedate, String format) {
if (thedate != null) {
Instant instant = thedate.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
return localDateTime.format(dateTimeFormatter);
}
return null;
}
/**
* 日期转字符串
*
* @return String
* @author kevin
*/
public static String parseDateToString(Date thedate, String format, String zoneId) {
if (thedate != null) {
Instant instant = thedate.toInstant();
ZoneId zone = ZoneOffset.of(zoneId);
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
return localDateTime.format(dateTimeFormatter);
}
return null;
}
/**
* parseDateToString(Date thedate, String format)的重载方法
*
* @param thedate
* @return
*/
public static String parseDateToString(Date thedate) {
// String format = "yyyy-MM-dd";
return parseDateToString(thedate, DATE_FORMAT_FULL);
}
/**
* parseDateToString(Date thedate, String format)的重载方法
*
* @param thedate
* @return
*/
public static String parseDateToStringByZoneId(Date thedate, String zoneId) {
// String format = "yyyy-MM-dd";
return parseDateToString(thedate, DATE_FORMAT_FULL,zoneId);
}
/**
* 字符串转日期
*
* @return Date
* @author kevin
*/
public static Date parseStringToDate(String thedate, String format) {
DateFormat sdf = new SimpleDateFormat(format);
Date dd1 = null;
try {
dd1 = sdf.parse(thedate);
} catch (ParseException e) {
e.printStackTrace();
}
return dd1;
}
/**
* 由String型日期转成format形式String
*
* @param format1 原先格式
* @param format2 转化格式
* @return String
* @author kevin
*/
public static String changeFormatDateString(String format1, String format2, String strDate) {
if (strDate == null) {
return "";
}
if (strDate.length() >= format1.length() && format1.length() >= format2.length()) {
return parseDateToString(parseStringToDate(strDate, format1), format2);
}
return strDate;
}
/**
* 得到当前日期的前N天时间 yyyymmdd
*
* @param format
* @param day
* @return
*/
public static String beforeNDaysDate(String format, int day) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
if (day > 0) {
return LocalDateTime.now().minusDays(day).format(dateTimeFormatter);
}
return null;
}
/**
* 获得N个月后的日期
* <p>
* theDate 日期
* <p>
* int month 月数
* <p>
* format 格式
*/
public static String afterNMonthDate(String theDate, int month, String format) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
return LocalDateTime.parse(theDate,dateTimeFormatter)
.plusMonths(month)
.format(dateTimeFormatter);
}
/**
* 得到N天后的日期
*
* @param theDate 某日期
* 格式 yyyy-MM-dd
* @param nDayNum N天
* @return String N天后的日期
* @author kevin
*/
public static String afterNDaysDate(String theDate, Integer nDayNum, String format) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
return LocalDateTime.parse(theDate,dateTimeFormatter)
.plusDays(nDayNum)
.format(dateTimeFormatter);
}
/**
* 得到N小时后的日期
*
* @param theDate 时间
* @param nHourNum N小时数
* @param format 时间格式
* @return
*/
public static String afterNHoursDate(String theDate, Integer nHourNum, String format) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
return LocalDateTime.parse(theDate,dateTimeFormatter)
.plusHours(nHourNum)
.format(dateTimeFormatter);
}
/**
* 得到N分钟后的日期
*
* @param theDate
* @param nMinNum
* @param format
* @return
*/
public static String afterNMinsDate(String theDate, Integer nMinNum, String format) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
return LocalDateTime.parse(theDate,dateTimeFormatter)
.plusMinutes(nMinNum)
.format(dateTimeFormatter);
}
/**
* 得到N秒后的日期
* @param theDate
* @param nSecNum
* @param format
* @return
*/
public static String afterNSecondsDate(String theDate, Integer nSecNum, String format) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
return LocalDateTime.parse(theDate,dateTimeFormatter)
.plusSeconds(nSecNum)
.format(dateTimeFormatter);
}
// 比较两个字符串格式日期大小,带格式的日期
public static boolean isBefore(String strdat1, String strdat2, String format) {
try {
Date dat1 = parseStringToDate(strdat1, format);
Date dat2 = parseStringToDate(strdat2, format);
return dat1.before(dat2);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
// 比较两个字符串格式日期大小,带格式的日期,返回int
public static long isBefore_int(String strdat1, String strdat2, String format) {
long result = 0;
try {
Date dat1 = parseStringToDate(strdat1, format);
Date dat2 = parseStringToDate(strdat2, format);
return dat2.getTime() - dat1.getTime();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/*
* 得到上一个月或者下一个月的日期
*/
public static String getDayafterMonth(String theDate, int month, String formatStr) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatStr);
return LocalDateTime.parse(theDate).plusMonths(month).format(dateTimeFormatter);
/* Calendar now = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
Date dat1 = parseStringToDate(theDate, formatStr);
now.setTime(dat1);
sdf.setTimeZone(TimeZone.getDefault());
now.add(Calendar.MONTH, month);
return sdf.format(now.getTime());*/
}
/**
* 将秒转换为小时分秒等
*
* @param sec
* @return
*/
public String changeTime(int sec) {
String temp = "";
if (sec < 60) {
temp = "" sec "秒";
} else if (sec < 3600) {
temp = "" sec / 60 "分" sec % 60 "秒";
} else {
temp = "" sec / 3600 "小时" (sec % 3600) / 60 "分" sec % 60 "秒";
}
return temp;
}
/**
* 方法描述:
* 计算两个日期相差天数
*
* @param end 结束日期
* @param start 开始日期
* @return 作者:liangxuekai
* 日期:2018 2018年1月31日
*/
public static int getSubDays(String end, String start) {
LocalDate startDate = LocalDate.parse(start);
LocalDate endDate = LocalDate.parse(end);
Long between = ChronoUnit.DAYS.between(startDate, endDate);
return between.intValue();
}
/**
* 方法描述:
* 计算两个日期相差天数2
*
* @param endDate 结束日期
* @param startDate 开始日期
* @return 作者:liangxuekai
* 日期:2018 2018年1月31日
*/
public static int getSubDays(LocalDate endDate, LocalDate startDate) {
Long between = ChronoUnit.DAYS.between(startDate, endDate);
return between.intValue();
}
public static String getTimeDiff(Date time1, Date time2) throws Exception {
long l = time1.getTime() - time2.getTime();
String returnStr = "";
long day = l / (24 * 60 * 60 * 1000);
if (day > 0) {
returnStr = (day "天");
}
long hour = (l / (60 * 60 * 1000) - day * 24);
if (hour > 0) {
returnStr = (hour "小时");
}
long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
if (min > 0) {
returnStr = (min "分");
}
long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
if (s > 0) {
returnStr = (s "秒");
}
return returnStr;
}
/**
* 格式化时间-默认yyyy-MM-dd HH:mm:ss格式
*
* @param dateTime LocalDateTime对象
* @param pattern 要格式化的字符串
* @return
*/
public static String formatDateTime(LocalDateTime dateTime, String pattern) {
if (dateTime == null) {
return null;
}
return dateTime.format(DateTimeFormatter.ofPattern(StringUtils.isEmpty(pattern) ? DATE_FORMAT_FULL : pattern));
}
public static String formatDateTime(LocalDateTime dateTime) {
return formatDateTime(dateTime, DATE_FORMAT_FULL);
}
/**
* 获取某天的00:00:00
*
* @param dateTime
* @return
*/
public static String getDayStart(LocalDateTime dateTime) {
return formatDateTime(dateTime.with(LocalTime.MIN));
}
public static String getDayStart() {
return getDayStart(LocalDateTime.now());
}
/**
* 获取某天的23:59:59
*
* @param dateTime
* @return
*/
public static String getDayEnd(LocalDateTime dateTime) {
return formatDateTime(dateTime.with(LocalTime.MAX));
}
public static String getDayEnd() {
return getDayEnd(LocalDateTime.now());
}
/**
* 获取某月第一天的00:00:00
*
* @param dateTime LocalDateTime对象
* @return
*/
public static String getFirstDayOfMonth(LocalDateTime dateTime) {
return formatDateTime(dateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN));
}
public static String getFirstDayOfMonth() {
return getFirstDayOfMonth(LocalDateTime.now());
}
/**
* 获取某月最后一天的23:59:59
*
* @param dateTime LocalDateTime对象
* @return
*/
public static String getLastDayOfMonth(LocalDateTime dateTime) {
return formatDateTime(dateTime.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX));
}
public static String getLastDayOfMonth() {
return getLastDayOfMonth(LocalDateTime.now());
}
/**
* 获取某年第一天的00:00:00
*
* @param dateTime LocalDateTime对象
* @return
*/
public static String getFirstDayOfYear(LocalDateTime dateTime) {
return formatDateTime(dateTime.with(TemporalAdjusters.firstDayOfYear()).with(LocalTime.MIN));
}
public static String getFirstDayOfYear() {
return getFirstDayOfYear(LocalDateTime.now());
}
/**
* 获取某年最后一天的23:59:59
*
* @param dateTime LocalDateTime对象
* @return
*/
public static String getLastDayOfYear(LocalDateTime dateTime) {
return formatDateTime(dateTime.with(TemporalAdjusters.lastDayOfYear()).with(LocalTime.MAX));
}
public static String getLastDayOfYear() {
return getLastDayOfYear(LocalDateTime.now());
}
/**
* Date转LocalDate
* @param date
*/
public static LocalDate date2LocalDate(Date date) {
if(null == date) {
return null;
}
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
/**
* Date转LocalDateTime
* @param date
*/
public static LocalDateTime date2LocalDateTime(Date date) {
if(null == date) {
return null;
}
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}
/**
* LocalDate转Date
* @param localDate
* @return
*/
public static Date localDate2Date(LocalDate localDate) {
if(null == localDate) {
return null;
}
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
return Date.from(zonedDateTime.toInstant());
}
/**
* LocalDateTime转Date
* @param localDateTime
* @return
*/
public static Date localDateTime2Date(LocalDateTime localDateTime) {
if(null == localDateTime) {
return null;
}
ZonedDateTime zonedDateTime = localDateTime.toLocalDate().atStartOfDay(ZoneId.systemDefault());
return Date.from(zonedDateTime.toInstant());
}
public static String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static String SEQUENCE_FORMAT = "yyyyMMddHHmmss";
public static String DEFAULT_DATE = "yyyy-MM-dd";
public static String VN_DATE = "dd/MM/yyyy";
public static String formatNowWithDefaultFormat() {
return formatNow(DEFAULT_FORMAT);
}
/**
* @param pattern
* @return e.g. 2018-08-22 22:55:17
*/
public static String formatNow(String pattern) {
LocalDateTime now = LocalDateTime.now();
return format(now, pattern);
}
/**
* format a date time to the given pattern
*
* @param dateTime
* @param pattern
* @return e.g. 2018-08-22 22:55:17
*/
public static String format(LocalDateTime dateTime, String pattern) {
if (org.springframework.util.StringUtils.isEmpty(pattern)) {
pattern = DEFAULT_FORMAT;
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return dateTime.format(formatter);
}
public static String formatDate(LocalDate date, String pattern) {
if (org.springframework.util.StringUtils.isEmpty(pattern)) {
pattern = DEFAULT_DATE;
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return date.format(formatter);
}
public static LocalDateTime parse(String strDateTime) {
return parse(strDateTime, DEFAULT_FORMAT);
}
/**
* parse a formatted date time string to LocalDateTime
*
* @param strDateTime
* @param pattern
* @return
*/
public static LocalDateTime parse(String strDateTime, String pattern) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return LocalDateTime.parse(strDateTime, formatter);
}
/**
* a formatted ISO 8601 date time string to LocalDateTime
*
* @param strDateTime
* @return
*/
public static LocalDateTime parseISODateTime(String strDateTime) {
if (org.springframework.util.StringUtils.isEmpty(strDateTime)) {
return null;
}
DateTimeFormatter ISO_OFFSET_DATE_TIME_X = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart().appendOffset(" HH", "Z").optionalEnd()
.toFormatter();
return LocalDateTime.parse(strDateTime, ISO_OFFSET_DATE_TIME_X);
}
/**
* parse a formatted date time string to LocalDate
*
* @param strDate
* @return
*/
public static LocalDate parseToDate(String strDate) {
int dateLength = DEFAULT_DATE.length();
if (org.springframework.util.StringUtils.isEmpty(strDate) || strDate.length() < dateLength) {
return null;
}
String dateStr = strDate.substring(0, dateLength);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DEFAULT_DATE);
return LocalDate.parse(dateStr, formatter);
}
public static LocalDate parseToDateFuzzy(String year, String month, String day) {
if (org.springframework.util.StringUtils.isEmpty(year)) {
return null;
}
if (org.springframework.util.StringUtils.isEmpty(month)) {
month = "01";
}
if (month.length() == 1) {
month = "0" month;
}
if (org.springframework.util.StringUtils.isEmpty(day)) {
day = "01";
}
if (day.length() == 1) {
day = "0" day;
}
return parseToDate(year "-" month "-" day);
}
public static LocalDate parseAdvanceAIBirthDay(String birthDayStr, String separator) {
if (org.springframework.util.StringUtils.isEmpty(birthDayStr)) {
return null;
}
if (!birthDayStr.contains(separator)) {
return parseToDateFuzzy(birthDayStr, "", "");
} else if (birthDayStr.indexOf(separator) == birthDayStr.lastIndexOf(separator)) {
return parseToDateFuzzy(birthDayStr.split(separator)[1], birthDayStr.split(separator)[0], "");
} else {
return parseToDateFuzzy(birthDayStr.split(separator)[2], birthDayStr.split(separator)[1], birthDayStr.split(separator)[0]);
}
}
/**
* parse a formatted date time string to LocalDate
*
* @param strDate
* @return
*/
public static LocalDate parseToDate(String strDate, String pattern) {
int dateLength = pattern.length();
if (org.springframework.util.StringUtils.isEmpty(strDate) || strDate.length() < dateLength) {
return null;
}
String dateStr = strDate.substring(0, dateLength);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return LocalDate.parse(dateStr, formatter);
}
/**
* @param localDateTime
* @return 2018-08-22 22:55:17 -> 2018-08-22 00:00:00
*/
public static LocalDateTime beginOfDay(LocalDateTime localDateTime) {
return LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.of(0, 0, 0));
}
/**
* @param localDateTime
* @return 2018-10-23 19:00:00 -> 2018-10-23 23:59:59
*/
public static LocalDateTime endOfDay(LocalDateTime localDateTime) {
return LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.of(23, 59, 59));
}
/**
* @param localDateTime
* @return 2018-10-23 19:00:00 -> 2018-10-24 00:00:00
*/
public static LocalDateTime beginOfTomorrow(LocalDateTime localDateTime) {
return LocalDateTime.of(localDateTime.plusDays(1).toLocalDate(), LocalTime.of(0, 0, 0));
}
/**
* @param localDateTime
* @return 2018-10-23 19:00:00 -> 2018-10-22 00:00:00
*/
public static LocalDateTime beginOfYesterday(LocalDateTime localDateTime) {
return LocalDateTime.of(localDateTime.minusDays(1).toLocalDate(), LocalTime.of(0, 0, 0));
}
/**
* @param localDateTime
* @return 2018-10-23 19:00:00 -> 2018-10-22 23:59:59
*/
public static LocalDateTime endOfYesterday(LocalDateTime localDateTime) {
return LocalDateTime.of(localDateTime.minusDays(1).toLocalDate(), LocalTime.of(23, 59, 59));
}
public static LocalDateTime timestampToLocalDateTime(long timestamp) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneOffset.of(" 08"));
return localDateTime;
}
public static int getDayInterval(LocalDateTime start, LocalDateTime end) {
LocalDate startDate = start.toLocalDate();
LocalDate endDate = end.toLocalDate();
return (int) ChronoUnit.DAYS.between(startDate, endDate);
}
public static int getDayInterval(LocalDate start, LocalDate end) {
return (int) ChronoUnit.DAYS.between(start, end);
}
public static Boolean checkIdCardExpiry(LocalDate expiryDate) {
return getDayInterval(LocalDate.now(),expiryDate)<0;
}
public static long getAgeFromBirthday(LocalDate birthday) {
return getAgeFromBirthday(birthday, LocalDate.now());
}
public static long getAgeFromBirthday(LocalDate birthday, LocalDate toDate) {
return ChronoUnit.YEARS.between(birthday, toDate);
}
public static long getCurrentUnixTimestampForChina() {
return getCurrentUnixTimestamp(8);
}
public static long getCurrentUnixTimestampForVn() {
return getCurrentUnixTimestamp(7);
}
public static long getCurrentUnixTimestamp(int zoneOffsetHours) {
return LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(zoneOffsetHours));
}
public static String changeTZ(String tzFrom, String tzTo, String dateToChange, String pattern) {
ZoneId zoneFrom = ZoneId.of(tzFrom);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
LocalDateTime localtDateAndTime = LocalDateTime.parse(dateToChange, formatter);
ZonedDateTime dateAndTimeINeed = ZonedDateTime.of(localtDateAndTime, zoneFrom );
ZonedDateTime rezDate = dateAndTimeINeed.withZoneSameInstant(ZoneId.of(tzTo));
return formatter.format(rezDate);
}
public static String convertUsTime1(Date date){
return changeTZ("Asia/Shanghai","America/New_York", DateUtils4Java8.parseDateToString(date),"yyyy-MM-dd HH:mm:ss");
}
public static Date convertUsTime2(Date date){
return DateUtils.parseDate(changeTZ("Asia/Shanghai","America/New_York", DateUtils4Java8.parseDateToString(date),"yyyy-MM-dd HH:mm:ss"));
}
public static void main(String[] args) {
System.out.println(changeTZ("Asia/Shanghai","America/New_York","2021-01-19T02:52:08.302 0000","yyyy-MM-dd'T'HH:mm:ss.SSS' '0000"));
}
}