📄 datetimeutils.java
字号:
package com.set.utils;
/**
* @author Administrator
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
public class DateTimeUtils {
public static Timestamp getNowTimestamp() {
Date date = new Date();
return new java.sql.Timestamp(date.getTime());
}
public static java.sql.Date getNowSqlDate() {
Date date = new Date();
return new java.sql.Date(date.getTime());
}
public static String getCurrentYear() {
try {
Date date = new Date();
SimpleDateFormat shortdf = new SimpleDateFormat("yyyy");
return shortdf.format(date);
} catch (IllegalArgumentException iae) {
return "";
}
}
public static String getCurrentMonth() {
try {
Date date = new Date();
SimpleDateFormat shortdf = new SimpleDateFormat("MM");
return shortdf.format(date);
} catch (IllegalArgumentException iae) {
return "";
}
}
public static String getCurrentday() {
try {
Date date = new Date();
SimpleDateFormat shortdf = new SimpleDateFormat("dd");
return shortdf.format(date);
} catch (IllegalArgumentException iae) {
return "";
}
}
public static final String getCurrentDate() {
try {
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.format(date);
} catch (IllegalArgumentException iae) {
return "";
}
}
public static final String toChineseString(Date date) {
try {
SimpleDateFormat shortdf = new SimpleDateFormat("yyyy年MM月dd日");
return shortdf.format(date);
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
return "";
}
}
public static final String dateToShortString(Date date) {
return dateToShortString(date, "yyyy-MM-dd");
}
public static final String dateToShortString(Date date, String format) {
try {
SimpleDateFormat shortdf = new SimpleDateFormat(format);
return shortdf.format(date);
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
return "";
}
}
public static final String dateToLangString(Date date) {
try {
SimpleDateFormat longdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return longdf.format(date);
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
return "";
}
}
public static final java.sql.Timestamp StrToTimestamp(String date) {
try {
SimpleDateFormat longdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return new java.sql.Timestamp(longdf.parse(date).getTime());
} catch (Exception iae) {
iae.printStackTrace();
return java.sql.Timestamp.valueOf("1970-01-01 00:00:01.000000000");
}
}
public static final java.sql.Date StrToSqlDate(String date) {
try {
return java.sql.Date.valueOf(date);
} catch (Exception iae) {
iae.printStackTrace();
return java.sql.Date.valueOf("1970-01-01");
}
}
/**
* Formats a Date as a fifteen character long String made up of the Date's
* padded millisecond value.
*
* @return a Date encoded as a String.
*/
public static final String dateToMillis(Date date) {
return StringUtils.zeroPadString(Long.toString(date.getTime()), 15);
}
public static final long dateMillis(String date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
long val = 0;
try {
val = format.parse(date).getTime();
} catch (Exception ex) {
}
return val;
}
public static final long dateMillis2(String date) {
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
long val = 0;
try {
val = format.parse(date).getTime();
} catch (Exception ex) {
}
return val;
}
public static final long dateLangMillis(String date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
long val = 0;
try {
val = format.parse(date).getTime();
} catch (Exception ex) {
}
return val;
}
public static final long dateLangLangMillis(String date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long val = 0;
try {
val = format.parse(date).getTime();
} catch (Exception ex) {
}
return val;
}
public static final long dateTolong(String date) {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
long val = 0;
try {
val = format.parse(date).getTime();
} catch (Exception ex) {
}
return val;
}
public static final Date long2Date(String longVal) {
Date date = new Date(Long.parseLong(longVal));
return date;
}
/**
* Default style for dates and times.
*/
public static int DEFAULT = DateFormat.DEFAULT;
/**
* Short style for dates and times.
*/
public static int SHORT = DateFormat.SHORT;
/**
* Medium style for dates and times.
*/
public static int MEDIUM = DateFormat.MEDIUM;
/**
* Long style for dates and times.
*/
public static int LONG = DateFormat.LONG;
/**
* Full style for dates and times.
*/
public static int FULL = DateFormat.FULL;
/**
* Default lenient setting for getDate.
*/
private static boolean LENIENT_DATE = false;
/**
* A "default" date format.
*/
public static String ESCAPE_DATE_PATTERN = "yyyy-mm-dd";
/**
* Convert String to Date using given format. Returns null if conversion
* fails. Set lenient=false to disallow dates like 2001-9-32.
* http://java.sun.com/j2se/1.4/docs/api/java/text/SimpleDateFormat.html
*
* @author Hal Deadman
*/
public static Date getDate(String dateDisplay, String format,
boolean lenient) {
if (dateDisplay == null) {
return null;
}
DateFormat df = null;
try {
if (format == null) {
df = new SimpleDateFormat();
} else {
df = new SimpleDateFormat(format);
}
// setLenient avoids allowing dates like 9/32/2001
// which would otherwise parse to 10/2/2001
df.setLenient(false);
return df.parse(dateDisplay);
} catch (ParseException e) {
return null;
}
}
/**
* Convert String to Date using given format. Returns null if conversion
* fails. Uses "strict" coNversion (lenient=false).
*
* @author Hal Deadman
*/
public static Date getDate(String dateDisplay, String format) {
return getDate(dateDisplay, format, LENIENT_DATE);
}
/**
* Convert String to Date using a medium (weekday day month year) format.
* Returns null if conversion fails. Uses "strict" coNversion
* (lenient=false).
*
* @author Hal Deadman
*/
public static Date getDate(String dateDisplay) {
return getDate(dateDisplay, null, LENIENT_DATE);
}
/**
* Return Date value using a String. Null or conversion error returns null.
*
* @param String
* representing Date
*/
public static Date toDate(String string) {
if (string == null) {
return null;
} else {
try {
return getDate(string);
} catch (Throwable t) {
return null;
}
}
}
/**
* Convert date to String for given locale in given style. A null locale
* will return the default locale.
*/
public static String getDate(Date date, Locale locale, int style) {
if (locale == null) {
return (DateFormat.getDateInstance(style).format(date));
}
return (DateFormat.getDateInstance(style, locale).format(date));
}
/**
* Convert date to String for default locale in given style. A null locale
* will return the default locale.
*/
public static String getDate(Date date, int style) {
return getDate(date, (Locale) null, style);
}
/**
* Convert date to String for default locale in DEFAULT style. A null locale
* will return the default locale.
*/
public static String getDate(Date date) {
return getDate(date, (Locale) null, DEFAULT);
}
/**
* Return String value representing Date. Null returns null.
*
* @param Date
*/
public static String toString(Date date) {
if (date == null) {
return null;
} else {
return getDate(date);
}
}
/**
* Return String value representing Date. Null returns null.
*
* @param Date
*/
public static String toEscape(Date date) {
if (date == null) {
return null;
}
DateFormat df = null;
try {
df = new SimpleDateFormat(ESCAPE_DATE_PATTERN);
} catch (Throwable t) {
return null;
}
df.setLenient(false);
return df.format(date);
}
public static final String DATE_SEPARATOR = "-";
public static final String TIME_SEPARATOR = ":";
public static final String STRING_SPACE = " ";
public static final String DATE_TIME_SEPARATOR = STRING_SPACE;
/**
* String to prepend to time [HH:MM:SS.d] to create a Timestamp escape
* string ["0004-06-30"].
*/
public static final String TIMESTAMP_DATE_ZERO = "0004-06-30";
/**
* String to append to date [YEAR-MM-DD] to create a Timestamp escape string ["
* 00:00:00.0"]. Note: includes leading space.
*/
public static final String TIMESTAMP_TIME_ZERO = " 00:00:00.0";
/**
* Escape string representing
*/
public static String ZERO_TIMESTAMP_DISPLAY = "4-06-30 00:00:00";
/**
* Timestamp representing ""November 30, 0002 00:00:00".
*/
public static Timestamp ZERO_TIMESTAMP = new Timestamp(
(long) 00000000000000);
public static String NULL_TIMESTAMP_DISPLAY = "1970-01-01 00:00:00";
/**
* Value needed to create Timestamp representing "January 1, 1970 00:00:00".
* From the documentation, you would think this would be Timestamp(0), but
* empirical tests show it to be Timestamp(18000000).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -