import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
@Component
public class DateUtil {
/**
* utc转本地时间
*
* @param time
* @return
*/
public static String utcToLocal(String time) {
ZonedDateTime zdt = ZonedDateTime.parse(time);
LocalDateTime localDateTime = zdt.toLocalDateTime();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return formatter.format(localDateTime.plusHours(8));
}
/**
* utc转本地时间
*
* @param time
* @return
*/
public static String utcToLocal(String time,String expression) {
ZonedDateTime zdt = ZonedDateTime.parse(time);
LocalDateTime localDateTime = zdt.toLocalDateTime();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(expression);
return formatter.format(localDateTime.plusHours(8));
}
public static String uTCToLocal(String utc) {
SimpleDateFormat sdfUTC = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Date parse =null;
try {
parse = sdfUTC.parse(utc);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = formatter.format(parse);
return format;
}
/**
* 本地时间转UTC
*
* @param time
* @return
*/
public static String localToUtc(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
Date parse;
try {
try {
parse = sdf.parse(time);
} catch (Exception e) {
parse = sdf2.parse(time);
}
SimpleDateFormat sdfUTC = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdfUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
return sdfUTC.format(parse);
} catch (Exception e) {
return null;
}
}
/**
* 获取时间本地格式
*
* @return
*/
public static String getLocalTimeByNow() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}
/**
* 获取时间本地格式
*
* @return
*/
public static String getTimeByNow() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
return sdf.format(date);
}
/**
* 获取时间UTC格式
*
* @return
*/
public static String getUtcTimeByNow() {
return localToUtc(getLocalTimeByNow());
}
/**
* 计算 day 天后的时间
*
* @param date
* @param day
* @return
*/
public static Date getDay(Date date, int day) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, day);
return calendar.getTime();
}
/**
* 计算 month 月后的时间
*
* @param date
* @param month
* @return
*/
public static Date getMonth(Date date, int month) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, month);
return calendar.getTime();
}
/**
* 计算 year 年后的时间
*
* @param date
* @param year
* @return
*/
public static Date getYear(Date date, int year) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.YEAR, year);
return calendar.getTime();
}
/**
* 根据单位 年月日 来计算date时间num单位后的时间
*
* @param date 计算的时间 yyyy-MM-dd
* @param num 数量
* @param unit 单位年月日
* @return
*/
public static String getDate_day_month_year(String date, String num, String unit) throws Exception {
String dateStr = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date parse = sdf.parse(date);
if (unit.equals("年")) {
Date year = getYear(parse, Integer.valueOf(num));
dateStr = sdf.format(year);
} else if (unit.equals("月")) {
Date month = getMonth(parse, Integer.valueOf(num));
dateStr = sdf.format(month);
} else if (unit.equals("天")) {
Date day = getDay(parse, Integer.valueOf(num));
dateStr = sdf.format(day);
}
return dateStr;
}
public static void main(String[] args) throws ParseException {
String s = uTCToLocal("2022-04-07T16:00:00.000Z");
System.out.println("s = " + s);
}
}
java时间转换
最新推荐文章于 2023-06-13 11:46:34 发布