package df.util.type;
import android.util.Log;
import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 时间相关的工具类
*/
public class TimeUtil {
public static final String TAG = "Quwei.TimeUtil";
public static final long SECOND = 1000;
public static final long MINUTE = SECOND * 60;
public static final long HOUR = MINUTE * 60;
public static final long DAY = HOUR * 24;
public static final long WEEK = DAY * 7;
/**
* 时间格式。
*/
public final static String theTimeFormat = "yyyy-MM-dd HH:mm:ss";
public final static String otherTimeFormat = "yyyyMMddHHmmss";
public static final boolean useFastDateFormatter = true;
private final static SimpleDateFormat theTimeFormator = new SimpleDateFormat(theTimeFormat);
//???? TODO: 只读,因此不进行同步控制
private static HashMap theFastTimeFormatterMap = new HashMap();
static {
theFastTimeFormatterMap.put("yyyyMMddHHmmss", new FastDateFormatOfyyyyMMddHHmmss()); //new SimpleDateFormat("yyyyMMddHHmmss") );
theFastTimeFormatterMap.put("yyyy-MM-dd HH:mm:ss", new FastDateFormatOfyyyy_MM_dd_HH_mm_ss()); //new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") );
theFastTimeFormatterMap.put("yyyy/MM/dd HH:mm:ss", new FastDateFormatOfyyyy__MM__dd_HH_mm_ss()); //new SimpleDateFormat("yyyy/MM/dd HH:mm:ss") );
theFastTimeFormatterMap.put("yyyyMMdd", new FastDateFormatOfyyyyMMdd()); // new SimpleDateFormat("yyyyMMdd") );
theFastTimeFormatterMap.put("yyyy-MM-dd", new FastDateFormatOfyyyy_MM_dd()); // new SimpleDateFormat("yyyy-MM-dd") );
theFastTimeFormatterMap.put("yyyy/MM/dd", new FastDateFormatOfyyyy__MM__dd()); // new SimpleDateFormat("yyyy/MM/dd") );
theFastTimeFormatterMap.put("HHmmss", new FastDateFormatOfHHmmss()); //new SimpleDateFormat("HHmmss") );
theFastTimeFormatterMap.put("HH:mm:ss", new FastDateFormatOfHH_mm_ss()); //new SimpleDateFormat("HH:mm:ss") );
theFastTimeFormatterMap.put("HH", new FastDateFormatOfHH()); //new SimpleDateFormat("HH") );
}
/**
* 把毫秒转换为分钟
*
* @return long
*/
public static final long millisecondToMinute(long millsecondArg) {
long v_minute = 0;
v_minute = millsecondArg / (1000 * 60);
return v_minute;
}
public static final DateFormat getTimeFormatter(String format) {
//???? TODO: 直接使用SimpleDateFormat时,发现这种方式在多线程情况下出现不正确,
//???? TODO: 可能是SimpleDateFormat的问题。 因此,我们采用了自己的Fast实现
if (useFastDateFormatter) {
DateFormat sdf = (DateFormat) theFastTimeFormatterMap.get(format);
if (sdf != null)
return sdf;
}
return new SimpleDateFormat(format);
}
///////////////////////////////////////////////////////////////////
public static SimpleDateFormat newTimeFormatter(String format) {
return new SimpleDateFormat(format);
}
/**
* 参数格式为"1:00:23 AM GMT+08:00",返回“0100”
*/
public static String toHHmmFromHHmmssaz(String time) {
try {
SimpleDateFormat tf = new SimpleDateFormat("hh:mm:ss a z", Locale.ENGLISH);
Calendar cal = TimeUtil.toRawCalendar(time, tf);
String time2 = TimeUtil.toString(cal, "HHmm");
return time2;
} catch (Exception e) {
Log.e(TAG, "toHHmmFromHHmmssaz failure", e);
return "";
}
}
public static String toHHmmFromHHmmssazForDojoTimePicker(String time) {
//TODO: "dojo.widget.DropdownTimePicker"类232行有bug,更改后不需要转换了
//return toHHmmFromHHmmssaz(time);
return time;
}
/**
* 根据日期内容及日期格式判断该日期是否合法
*
* @param content
* @param format
* @return 合法返回true,非法返回false
*/
public static boolean isRightDate(String content, String format) {
boolean ret = false;
if (!StringUtil.empty(content) && !StringUtil.empty(format)) {
try {
toRawCalendar(content, format);
ret = true;
} catch (Exception e) {
}
}
return ret;
}
/**
* 判断时间是否相等。
* 由于系统中的时间不需要很高的精度,因此我们提供了特定的比较方法。
*/
public static boolean equals(Calendar c1, Calendar c2) {
if (c1 == c2)
return true;
if ((null == c1) || (null == c2))
return false;
final long t1 = c1.getTime().getTime() / 1000;
final long t2 = c2.getTime().getTime() / 1000;
return (t1 == t2);
/*
return ((c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR))
&& (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH))
&& (c1.get(Calendar.DAY_OF_MONTH) == c2.get(Calendar.DAY_OF_MONTH))
&& (c1.get(Calendar.HOUR_OF_DAY) == c2.get(Calendar.HOUR_OF_DAY))
&& (c1.get(Calendar.MINUTE) == c2.get(Calendar.MINUTE))
&& (c1.get(Calendar.SECOND) == c2.get(Calendar.SECOND)));
*/
}
/**
* 复制Calendar对象。
*/
public static Calendar clone(Calendar cal) {
Calendar c = Calendar.getInstance();
c.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY),
cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
c.set(Calendar.MILLISECOND, cal.get(Calendar.MILLISECOND));
return c;
}
/**
* 判断输入的日期是否合法
*
* @param time
* @return
*/
public static boolean isValidateTime(long time) {
boolean validate = false;
Calendar cal = TimeUtil.toCalendar(time, "yyyyMMddHHmmss");
long timeL = TimeUtil.toNumber(cal, "yyyyMMddHHmmss");
if (time == timeL) {
validate = true;
}
return validate;
}
///////////////////////////////////////////////////////////////////
public static String toString(Calendar cal, DateFormat formator) {
if (null == cal)
return "";
assert(formator != null);
return formator.format(cal.getTime());
}
public static String toString(Date date, DateFormat formator) {
if (null == date)
return "";
assert(formator != null);
return formator.format(date);
}
public static String toString(Calendar cal, String format) {
if (null == cal)
return "";
//GeneralAssertion.assertion(!StringUtil.empty(format));
return toString(cal, getTimeFormatter(format));
}
public static String toString(Date date, String format) {
if (null == date)
return "";
//GeneralAssertion.assertion(!StringUtil.empty(format));
return toString(date, getTimeFormatter(format));
}
public static String toString(String format) {
return toString(Calendar.getInstance(), format);
}
public static String toString(String time, String fromFormat, String toFormat) {
//???? TODO 性能不高
return toString(toCalendar(time, fromFormat), toFormat);
}
/**
* 如果时间错误,返回“”字符串
*/
public static String toStringEmpty(String time, String fromFormat, String toFormat) {
Calendar cal = null;
try {
cal = toRawCalendar(time, getTimeFormatter(fromFormat));
} catch (Exception ex) {
}
//???? TODO 性能不高
return toString(cal, toFormat);
}
public static String toString(Calendar cal) {
return toString(cal, theTim