java时间类型转换方法
1. Calendar 转 LocalDateTime
/**
* Calendar 转 LocalDateTime
* @param calendar
* @return
*/
public static LocalDateTime calenderToLocalDateTime(Calendar calendar){
if (calendar == null) {
return null;
}
return LocalDateTime.ofInstant(calendar.toInstant(), ZoneOffset.systemDefault());
}
/**
* 将Calendar转成LocalDateTime
* 第二种方法
*/
public static LocalDateTime getLocalDateTime(Calendar calendar) {
return LocalDateTime.of(
ToolForDateTime.getYearByCalendar(calendar)
, ToolForDateTime.getMonthByCalendar(calendar)
, ToolForDateTime.getDayByCalendar(calendar)
, ToolForDateTime.getHourByCalendar(calendar)
, ToolForDateTime.getMinuteByCalendar(calendar)
, ToolForDateTime.getSecondByCalendar(calendar)
, 0
);
}
2. LocalDateTime转成Calendar
/**
* 将LocalDateTime转成Calendar
*/
public static Calendar getCalendarByLocalDateTime(LocalDateTime localDateTime) {
int year = localDateTime.getYear();
int month = localDateTime.getMonthValue() - 1;
int day = localDateTime.getDayOfMonth();
int hour = localDateTime.getHour();
int minute = localDateTime.getMinute();
int second = localDateTime.getSecond();
int millis = localDateTime.get(ChronoField.MILLI_OF_SECOND);
Calendar calendar = new Calendar.Builder() {{
setDate(year, month, day);
setTimeOfDay(hour, minute, second, millis);
}}.build();
return calendar;
}
3. date 转 LocalDateTime
/**
* date 转 LocalDateTime
* @param date
* @return
*/
public static LocalDateTime dateToLocalDateTime(Date date) {
if( date == null ){
return null;
}
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}
/**
* date 转 LocalDateTime
* 方式二
* @param date
* @return
*/
public static LocalDateTime dateToLocalDateTime(Date date) {
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, zoneId);
}
4. LocalDate转Date
/**
* 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());
}
5. LocalDateTime 转换为string
/**
* 常用的时间格式
*/
public static final String format_DateTime = "yyyy-MM-dd HH:mm:ss";
public static final String format_Date = "yyyy-MM-dd";
/**
* LocalDateTime 转换为string
* yyyy-MM-dd HH:mm:ss SSS
*/
public static String LocalDateTimeToString(LocalDateTime localDateTime){
DateTimeFormatter df = DateTimeFormatter.ofPattern(format_DateTime);
String result = df.format(localDateTime);
return result;
}
/**
* LocalDateTime 转换为string
* yyyy-MM-dd
*/
public static String LocalDateTimeToStringToDate(LocalDateTime localDateTime){
DateTimeFormatter df = DateTimeFormatter.ofPattern(format_Date);
String result = df.format(localDateTime);
return result;
}
6. string 转换为LocalDateTime
/**
* 常用的时间格式
*/
public static final String format_DateTime = "yyyy-MM-dd HH:mm:ss";
/**
* string 转换为LocalDateTime
*/
public static LocalDateTime stringToLocalTime(String str){
DateTimeFormatter df = DateTimeFormatter.ofPattern(format_DateTime);
LocalDateTime dateTime = LocalDateTime.parse(str, df);
return dateTime;
}
7. date转string
// date转string
public static String dateToString(Date date){
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
return ft.format(date);
}
8. string转date(只转换年月日,转换后时分秒默认:00:00:00)
/**
* string转date
* 只转换年月日
* @param str
* @return
* @throws ParseException
*/
public static Date stringToDate(String str) throws ParseException {
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
return ft.parse(str);
}
9. string转date(年月日时分秒)
/**
* 常用的时间格式
*/
public static final String format_DateTime = "yyyy-MM-dd HH:mm:ss";
/**
* string转date
* 转换 年月日时分秒
* @param str
* @return
* @throws ParseException
*/
public static Date stringToTdate(String str) throws ParseException {
return new SimpleDateFormat(format_DateTime).parse(str);
}
10. yyyy-MM-dd格式的字符串转化为 x年x月x日
/**
* yyyy-MM-dd转化为 x年x月x日
*/
public static String StringDateToYearMonthDay(String s){
String[] split = s.split("-");
return split[0] + "年" + split[1] + "月" + split[2] + "日";
}