jdatetime.java
来自「Struts2 + Spring JPA Hibernate demo.」· Java 代码 · 共 1,806 行 · 第 1/4 页
JAVA
1,806 行
*/
public static void resetDefaultMonthFix() {
defaultMonFix = true;
}
/**
* Sets month fix value.
*
* @param v
* month fix value
*/
public void setMonthFix(boolean v) {
monFix = new Boolean(v);
}
/**
* Resets month fix value to default one.
*/
public void resetMonthFix() {
monFix = null;
}
/**
* Returns actual mont fix value.
*/
public boolean getMonthFix() {
if (monFix != null) {
return monFix.booleanValue();
}
return defaultMonFix;
}
// ---------------------------------------------------------------- names
private static JdtNames defaultNames = new EnglishNames();
private JdtNames names = null;
/**
* Sets default JDateTime names.
*
* @param newNames
* names
*/
public static void setDefaultNames(JdtNames newNames) {
if (newNames != null) {
defaultNames = newNames;
}
return;
}
/**
* Resets default date time names to English.
*/
public static void resetDefaultNames() {
defaultNames = new EnglishNames();
}
/**
* Sets current date time names.
*
* @param newNames
*/
public void setNames(JdtNames newNames) {
if (newNames != null) {
names = newNames;
}
return;
}
/**
* Reset current names to default values.
*/
public void resetNames() {
names = null;
}
/**
* Returns actual date time names.
*
* @return actual date time names.
*/
public JdtNames getNames() {
if (names != null) {
return names;
}
return defaultNames;
}
// ---------------------------------------------------------------- formats
private static final String DEFAULT_FORMAT_TEMPLATE = "YYYY-MM-DD hh:mm:ss.mss";
private static String defaultFormatTemplate = DEFAULT_FORMAT_TEMPLATE;
private String formatTemplate = null;
/**
* Sets default format template.
*
* @param newFormatTemplate
*/
public static void setDefaultFormatTemplate(String newFormatTemplate) {
if (newFormatTemplate != null) {
defaultFormatTemplate = newFormatTemplate;
}
return;
}
/**
* Resets default format to JDateTime defaults format: YYYY-MM-DD hh:mm:s.m
*/
public static void resetDefaultFormatTemplate() {
defaultFormatTemplate = DEFAULT_FORMAT_TEMPLATE;
return;
}
/**
* Sets current format template.
*
* @param newFormat
*/
public void setFormatTemplate(String newFormat) {
if (newFormat != null) {
formatTemplate = newFormat;
}
return;
}
/**
* Resets current format template to default value.
*/
public void resetFormatTemplate() {
formatTemplate = null;
}
/**
* Returns actual format template.
*
* @return actual format template
*/
public String getFormatTemplate() {
if (formatTemplate != null) {
return formatTemplate;
}
return defaultFormatTemplate;
}
// ----------------------------------------------------------------
// formatters
private static JdtFormatter formatter = new DefaultFormatter();
/**
* Sets default formatter
*
* @param f
* formatter instance
*/
public static void setDefaultFormatter(JdtFormatter f) {
if (f != null) {
formatter = f;
}
}
// ----------------------------------------------------------------
// formatters usage (String conversions)
/**
* Get current date/time in specified format.
*
* @param template
* format template
*
* @return current date/time string
*/
public String get(String template) {
return formatter.get(this, template);
}
/**
* Get current date/time in default format.
*
* @return current date/time string
*/
public String get() {
return get(getFormatTemplate());
}
/**
* Sets date/time from a string and specified template.
*
* @param s
* string containing date time information
* @param template
* format template
*/
public void set(String s, String template) {
DateTimeStamp dts = formatter.set(s, template);
if (dts != null) {
setDateTimeStamp(dts);
}
}
/**
* Sets date/time from a string and default template.
*
* @param s
* string containing date time information
*/
public void set(String s) {
set(s, getFormatTemplate());
}
// ---------------------------------------------------------------- week
// definitions
private static int defaultFirstDayOfWeek = 1;
private int firstDayOfWeek = 0;
private static int defaultMustHaveDayOfFirstWeek = 4;
private int mustHaveDayOfFirstWeek = 0;
/**
* Defines default week. Not valid values are ignored and may be used for
* individual settings of each of 2 input parameters.
*
* @param start
* first day in week
* @param must
* must have day of the 1st week
*/
public static void setDefaultWeekDefinition(int start, int must) {
if ((start >= 1) && (start <= 7)) {
defaultFirstDayOfWeek = start;
}
if ((must >= 1) && (must <= 7)) {
defaultMustHaveDayOfFirstWeek = must;
defaultMinimalDaysInFirstWeek = convertMin2Must(defaultFirstDayOfWeek, must);
}
}
/**
* Defines week.
*
* @param start
* first day in week
* @param must
* must have day of the 1st week
*/
public void setWeekDefinition(int start, int must) {
if ((start >= 1) && (start <= 7)) {
firstDayOfWeek = start;
}
if ((must >= 1) && (must <= 7)) {
mustHaveDayOfFirstWeek = must;
minimalDaysInFirstWeek = convertMin2Must(getFirstDayOfWeek(), must);
}
}
/**
* Resets default week definition.
*/
public static void resetDefaultWeekDefinition() {
defaultFirstDayOfWeek = 1;
defaultMustHaveDayOfFirstWeek = 4;
}
/**
* Resets week definition.
*/
public void resetWeekDefintion() {
firstDayOfWeek = 0;
mustHaveDayOfFirstWeek = 0;
}
/**
* Returns the first day of the week.
*
* @return first day of week
*/
public int getFirstDayOfWeek() {
if (firstDayOfWeek != 0) {
return firstDayOfWeek;
}
return defaultFirstDayOfWeek;
}
/**
* Returns must have day of the 1st week.
*
* @return must have day of the first week
*/
public int getMustHaveDayOfFirstWeek() {
if (mustHaveDayOfFirstWeek != 0) {
return mustHaveDayOfFirstWeek;
}
return defaultMustHaveDayOfFirstWeek;
}
// ---------------------------------------------------------------- week
// definitions (alt)
private static int defaultMinimalDaysInFirstWeek = 4;
private int minimalDaysInFirstWeek = 0;
/**
* Returns minimal number of days of the first week. It is calculated from
* must have day of the first week.
*
* @return minimal number of days of the first week
*/
public int getMinimalDaysInFirstWeek() {
if (minimalDaysInFirstWeek != 0) {
return minimalDaysInFirstWeek;
}
return defaultMinimalDaysInFirstWeek;
}
/**
* Defines default week alternatively.
*
* @param start
* first day in week
* @param min
* minimal days of week
*/
public static void setDefaultWeekDefinitionAlt(int start, int min) {
if ((start >= 1) && (start <= 7)) {
defaultFirstDayOfWeek = start;
}
if ((min >= 1) && (min <= 7)) {
defaultMustHaveDayOfFirstWeek = convertMin2Must(defaultFirstDayOfWeek, min);
defaultMinimalDaysInFirstWeek = min;
}
}
/**
* Defines week alternatively.
*
* @param start
* first day in week
* @param min
* minimal days of week
*/
public void setWeekDefinitionAlt(int start, int min) {
if ((start >= 1) && (start <= 7)) {
firstDayOfWeek = start;
}
if ((min >= 1) && (min <= 7)) {
mustHaveDayOfFirstWeek = convertMin2Must(getFirstDayOfWeek(), min);
minimalDaysInFirstWeek = min;
}
}
/**
* Converts minimal day of week to must have day of week. Method is
* symmetrical.
*
* @param start
* first day of week
* @param min
* minimal day of week
*
* @return must have day of week
*/
private static int convertMin2Must(int start, int min) {
int must = 8 - min + (start - 1);
if (must > 7) {
must -= 7;
}
return must;
}
// ---------------------------------------------------------------- isValid
/**
* Checks if some string represents a valid date. It uses
* <code>JDateTime</code>'s default format template.
*
* @param s
* string
*
* @return true if date is valid, otherwise false
*/
public static boolean isValid(String s) {
return isValid(s, defaultFormatTemplate);
}
/**
* Checks if some string represents a valid date.
*
* @param s
* string
* @param template
* template
*
* @return true if date is valid, otherwise false
*/
public static boolean isValid(String s, String template) {
DateTimeStamp dtsOriginal = formatter.set(s, template);
if (dtsOriginal == null) {
return false;
}
return TimeUtil.isValidDateTime(dtsOriginal);
// JDateTime jdt = new JDateTime(dtsOriginal);
// DateTimeStamp dtsMatch = jdt.getDateTimeStamp();
// return (dtsOriginal.compareTo(dtsMatch) == 0);
}
// ---------------------------------------------------------------- toString
/**
* Returns date time string in YYYY-MM-DD hh:mm:s.m format.
*
* @return date time string in default format
* @see #get
*/
public String toString() {
StringBuffer sb = new StringBuffer(25);
sb.append(time.year).append('-');
if (time.month < 10)
sb.append('0');
sb.append(time.month).append('-');
if (time.day < 10)
sb.append('0');
sb.append(time.day).append(' ');
if (time.hour < 10)
sb.append('0');
sb.append(time.hour).append(':');
if (time.minute < 10)
sb.append('0');
sb.append(time.minute).append(':');
if (time.second < 10)
sb.append('0');
sb.append((int) time.second).append('.');
int milis = getMillisecond();
if (milis < 10)
sb.append('0');
if (milis < 100)
sb.append('0');
sb.append(milis);
return sb.toString();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?