|
@@ -0,0 +1,89 @@
|
|
|
|
|
+package nckd.jxccl.base.common.utils;
|
|
|
|
|
+
|
|
|
|
|
+import java.text.ParseException;
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.Locale;
|
|
|
|
|
+import java.util.TimeZone;
|
|
|
|
|
+
|
|
|
|
|
+public class DateTimeUtils {
|
|
|
|
|
+
|
|
|
|
|
+ public static String format(Date d, String fmt) {
|
|
|
|
|
+ return format(d, fmt, (TimeZone)null);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static String format(Date d, String fmt, TimeZone timezone) {
|
|
|
|
|
+ return format(d, fmt, timezone, (Locale)null);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static String format(Date d, String fmt, TimeZone timezone, Locale locale) {
|
|
|
|
|
+ if (fmt == null) {
|
|
|
|
|
+ fmt = "yyyy-MM-dd HH:mm:ss";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ SimpleDateFormat df;
|
|
|
|
|
+ if (locale == null) {
|
|
|
|
|
+ df = new SimpleDateFormat(fmt);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ df = new SimpleDateFormat(fmt, locale);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (timezone == null) {
|
|
|
|
|
+ df.setTimeZone(TimeZone.getDefault());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ df.setTimeZone(timezone);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return df.format(d);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static Date parseDate(String s) throws ParseException {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return parseDate(s, "yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
+ } catch (ParseException var6) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return parseDate(s, "yyyy-MM-dd");
|
|
|
|
|
+ } catch (ParseException var5) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return parseDate(s, "MM/dd/yyyy HH:mm:ss");
|
|
|
|
|
+ } catch (ParseException var4) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return parseDate(s, "MM/dd/yyyy");
|
|
|
|
|
+ } catch (ParseException var3) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return parseDate(s, "HH:mm:ss");
|
|
|
|
|
+ } catch (ParseException var2) {
|
|
|
|
|
+ throw new ParseException("can not understand your format", -1);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static Date parseDate(String s, String fmt) throws ParseException {
|
|
|
|
|
+ return parseDate(s, fmt, (TimeZone)null);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static Date parseDate(String s, String fmt, TimeZone timezone) throws ParseException {
|
|
|
|
|
+ return parseDate(s, fmt, timezone, (Locale)null);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static Date parseDate(String s, String fmt, TimeZone timezone, Locale locale) throws ParseException {
|
|
|
|
|
+ SimpleDateFormat df;
|
|
|
|
|
+ if (locale == null) {
|
|
|
|
|
+ df = new SimpleDateFormat(fmt);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ df = new SimpleDateFormat(fmt, locale);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (timezone == null) {
|
|
|
|
|
+ timezone = TimeZone.getDefault();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ df.setTimeZone(timezone);
|
|
|
|
|
+ df.setLenient(false);
|
|
|
|
|
+ return df.parse(s);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|