java.util.Date和String類型相互轉(zhuǎn)換
private static String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
- java.util.Date轉(zhuǎn)為String
public static String dateToString(Date date, String dateFormat) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String dateStr = sdf.format(date);
return dateStr;
}
- String轉(zhuǎn)為java.util.Date
public static Date stringToDate(String dateStr, String dateFormat) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static void main(String[] args) {
// java.util.Date轉(zhuǎn)為String
String dateStr = dateToString(new Date(), DATE_FORMAT);
System.out.println("dateStr: " + dateStr);
// String轉(zhuǎn)為java.util.Date
Date date = stringToDate(dateStr, DATE_FORMAT);
System.out.println("date: " + date);
}
java.util.Date與java.sql.Date類型轉(zhuǎn)換
- java.sql.Date轉(zhuǎn)為java.util.Date
public static Date sqlDateToDate(java.sql.Date sqlDate) {
Date date = new Date(sqlDate.getTime());
return date;
}
- java.util.Date轉(zhuǎn)為java.sql.Date
public static java.sql.Date dateToSqlDate(Date date) {
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
return sqlDate;
}
// java.util.Date轉(zhuǎn)為java.sql.Date
java.sql.Date sqlDate = dateToSqlDate(new Date());
System.out.println("sqlDate: " + sqlDate);
// java.sql.Date轉(zhuǎn)為java.util.Date
Date utilDate = sqlDateToDate(sqlDate);
System.out.println("utilDate: " + utilDate);
java.sql.timestamp與String類型轉(zhuǎn)換
- java.sql.timestamp轉(zhuǎn)為String
public static String timestampToString(Timestamp time, String dateFormat) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String str = sdf.format(time);
return str;
}
- String轉(zhuǎn)為java.sql.timestamp
public static Timestamp stringToTimestamp(String strDate, String dateFormat) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
Date date = null;
try {
date = sdf.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
Timestamp timestamp = new Timestamp(date.getTime());
return timestamp;
}
// String轉(zhuǎn)為java.sql.timestamp
Timestamp timestamp = stringToTimestamp("2023-02-28 14:10:30", DATE_FORMAT);
System.out.println("timestamp: " + timestamp);
// java.sql.timestamp轉(zhuǎn)為String
String timestampStr = timestampToString(timestamp, DATE_FORMAT);
System.out.println("timestampStr: " + timestampStr);
LocalDate與Date類型轉(zhuǎn)換
public static Date localDateToDate(LocalDate localDate) {
Instant instant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant();
Date date = Date.from(instant);
return date;
}
public static LocalDate dateToLocalDate(Date date) {
Instant instant = date.toInstant();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
return localDateTime.toLocalDate();
}
// Date轉(zhuǎn)為LocalDate
LocalDate localDate = dateToLocalDate(new Date());
System.out.println("localDate: " + localDate);
// LocalDate轉(zhuǎn)為Date
Date date1 = localDateToDate(localDate);
System.out.println("date: " + date1);