📄 dateutil.java
字号:
package com.iplan.portal.framework.utils;
import org.springframework.context.i18n.LocaleContextHolder;
import com.iplan.portal.framework.Constants;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Date Utility 类
* <p>
* http://www.hao-se.cn
* </p>
*
* @author ws
*/
public class DateUtil
{
private static String defaultDatePattern = "yyyy-MM-dd";
public static String DATE_PATTERN = "yyyy/MM/dd";
static
{
Locale locale = LocaleContextHolder.getLocale();
//尝试试从messages_zh_Cn.properties中获取defaultDatePattner.
try
{
defaultDatePattern = ResourceBundle.getBundle(Constants.MESSAGE_BUNDLE_KEY, locale).getString("date.default_format");
}
catch (MissingResourceException mse)
{
;
}
}
/**
* get the default date pattern
*/
public static String getDatePattern()
{
return defaultDatePattern;
}
/**
* returns the current date in the default format
*/
public static String getToday()
{
Date today = new Date();
return format(today);
}
/**
* convert Date to String in default format
*/
public static String format(Date date)
{
return format(date, getDatePattern());
}
/**
* convert Date to String in pattern
*/
public static String format(Date date, String pattern)
{
String returnValue = "";
if (date != null)
{
SimpleDateFormat df = new SimpleDateFormat(pattern);
returnValue = df.format(date);
}
return (returnValue);
}
/**
* converts a String to a Date using the default Pattern
*/
public static Date parse(String strDate) throws ParseException
{
return parse(strDate, getDatePattern());
}
/**
* converts a String to a Date using the pattern
*/
public static Date parse(String strDate, String pattern) throws ParseException
{
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.parse(strDate);
}
/**
* add some month from the date
*/
public static Date addMonth(Date date, int n) throws Exception
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, n);
return cal.getTime();
}
/**
* This method attempts to convert an DB2-formatted date in the form
* yyyy-MM-dd to dd/MM/yyyy.
*
* @param aDate
* date from database as a string
* @return formatted string for the ui
*/
public static final String convertDateToString(Date aDate) {
return convertDateToString(DATE_PATTERN, aDate);
}
/**
*
* @param aMask
* @param aDate
* @return
*/
public static final String convertDateToString(String aMask, Date aDate) {
String returnValue = "";
if (aDate != null) {
SimpleDateFormat df = new SimpleDateFormat(aMask);
returnValue = df.format(aDate);
}
return (returnValue);
}
public static final Date convertStringToDate(String strDate) {
return convertStringToDate(DATE_PATTERN, strDate);
}
/**
* This method generates a string representation of a date/time in the
* format you specify on input
*
* @param aMask
* the date pattern the string is in
* @param strDate
* a string representation of a date
* @return a converted Date object
* @see java.text.SimpleDateFormat
*/
public static final Date convertStringToDate(String aMask, String strDate) {
SimpleDateFormat df = null;
Date date = null;
df = new SimpleDateFormat(aMask);
try {
date = df.parse(strDate);
} catch (ParseException pe) {
date = null;
}
return (date);
}
public static void main(String args[]){
String s = "订单跟踪系统.xls";
int index = s.lastIndexOf(".");
s = s.substring(index+1, s.length());
System.out.println(s);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -