public class DateAdapter { private Date date; public DateAdapter() { this.date = new Date(); } public DateAdapter(long timestamp) { this.date = new Date(timestamp); } public DateAdapter(int year, int month, int day) { Calendar calendar = Calendar.getInstance(); calendar.set(year, month - 1, day); this.date = calendar.getTime(); } public int getYear() { Calendar calendar = Calendar.getInstance(); calendar.setTime(this.date); return calendar.get(Calendar.YEAR); } public int compareTo(DateAdapter other) { return this.date.compareTo(other.date); } } public class Formatter { private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss"; public static String format(DateAdapter dateAdapter) { return format(dateAdapter, DEFAULT_PATTERN); } public static String format(DateAdapter dateAdapter, String pattern) { SimpleDateFormat sdf = new SimpleDateFormat(pattern); return sdf.format(dateAdapter.getDate()); } public static DateAdapter parse(String dateString) throws ParseException { return parse(dateString, DEFAULT_PATTERN); } public static DateAdapter parse(String dateString, String pattern) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat(pattern); Date date = sdf.parse(dateString); return new DateAdapter(date.getTime()); } } <dependency> <groupId>com.example</groupId> <artifactId>dateadapterj</artifactId> <version>1.0.0</version> </dependency>


上一篇:
下一篇:
切换中文