| | |
| | | import java.text.DateFormat; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDate; |
| | | import java.time.ZoneId; |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | import java.util.GregorianCalendar; |
| | |
| | | return new SimpleDateFormat("yyyy"); |
| | | } |
| | | }; |
| | | |
| | | public static final String STR_DATE = "yyyy-MM-dd"; |
| | | public static final String STRDATE = "yyyyMMdd"; |
| | | public static final String STR_YEAR_MONTH = "yyyy-MM"; |
| | | public static final String STRYEARMONTH = "yyyyMM"; |
| | | public static final String STR_DATE_TIME = "yyyy-MM-dd HH:mm:ss.SSS"; |
| | | public static final String STR_DATE_TIME_SMALL = "yyyy-MM-dd HH:mm:ss"; |
| | | public static final String STR_DD_MM_YYYY = "dd/MM/yyyy HH:mm:ss"; |
| | | public static final String STR_DATE_TIME_MIN = "yyyy-MM-dd HH:mm"; |
| | | public static final String STR_DATE_TIME_HOUR = "yyyy-MM-dd HH"; |
| | | public static final String STR_DATE_TIME_FULL = "yyyyMMddHHmmssSSS"; |
| | | public static final String STR_HHMMSS = "HH:mm:ss"; |
| | | public static final String STR_HHMM = "HH:mm"; |
| | | |
| | | public static String format(Date date, String format) { |
| | | SimpleDateFormat df = new SimpleDateFormat(format); |
| | | return df.format(date); |
| | | } |
| | | |
| | | public static String formattedDate(String dateString, String dateformat, String format) { |
| | | // 定义原始日期字符串和其格式 |
| | | SimpleDateFormat originalFormat = new SimpleDateFormat(dateformat); |
| | | // 定义目标日期格式 |
| | | SimpleDateFormat targetFormat = new SimpleDateFormat(format); |
| | | try { |
| | | // 解析原始日期字符串 |
| | | Date date = originalFormat.parse(dateString); |
| | | // 将日期格式化为目标格式 |
| | | String formattedDate = targetFormat.format(date); |
| | | |
| | | // 输出转换后的日期字符串 |
| | | System.out.println(formattedDate); |
| | | return formattedDate; |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 以毫秒表示的时间 |
| | | */ |
| | |
| | | return dateFormat.format(date); |
| | | } |
| | | |
| | | /** |
| | | * Date 转 LocalDate |
| | | * @param date |
| | | * @return |
| | | */ |
| | | public static LocalDate dateToLocalDate(Date date) { |
| | | return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * LocalDate 转 Date |
| | | * @param localDate |
| | | * @return |
| | | */ |
| | | public static Date localDateToDate(LocalDate localDate) { |
| | | return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); |
| | | } |
| | | } |