我有一些时间戳格式的
时间戳格式包含YYYY MM DD hh MM ss S AM/PM国家名称/城市名称(区域)
YYYY-MM-DD HH:mm:ss.S
YYYY-MM-DD HH:mm:ss.S AM/PM
YYYY-MM-DD HH:mm:ss.S AM/PM z
YYYY-MM-DD HH:mm:ss.z
我想对时间戳值进行验证,如果时间戳值是未来值(或大于现有值),则希望向用户显示通知消息。
对于日期格式,我写了以下代码,用于检查未来日期
DateTimeFormat df = DateTimeFormat.getFormat(dateFormat);
Date updateDate = df.parseStrict(newDateValue);
Date synchronizedDate = df.parseStrict(synchronizedDB_DateValue);
boolean isFutureDate = updateDate.after(synchronizedDate);
if (isFutureDate ) {
// send notification
}
else {
// do nothing
}
编辑:
以下代码仅适用于时间戳格式YYYY-MM-DD HH:MM:ss.S
String timestampFormat = "YYYY-MM-DD HH:mm:ss.S"; // It works
//String timestampFormat = "YYYY-MM-DD HH:mm:ss.S AM/PM";
//String timestampFormat = "YYYY-MM-DD HH:mm:ss.S AM/PM z"
//String timestampFormat = "YYYY-MM-DD HH:mm:ss.S z ";
String newTimestampFormat= timestampFormat.replaceAll("-", ".");
newTimestampFormat = newTimestampFormat.replace("YYYY", "yyyy");
newTimestampFormat = newTimestampFormat.replace("AM/PM", "a");
DateTimeFormat df = DateTimeFormat.getFormat(newTimestampFormat);
Date updateDate = df.parse(newTimeStampValue); // UPDATED VALUE = 2013.08.21 00:00:00.123
Date synchronizedDate = df.parseStrict(synchronizedDB_DateValue); // current or old db value = 2013.07.11 00:00:00.123
boolean isFutureTimestamp = updateDate.after(synchronizedDate);
if (isFutureTimestamp ) {
// send notification
}
else {
// do nothing
}
我需要对所有其他时间戳格式做哪些更改?
提前谢谢。
这是我的解决方案,它有效:)
private boolean function isFutureTimestamp(){
String gwtTimestampFormat = convertToGwtTimeStampFormat(timestampFormat);
// timestampFormat = YYYY-MM-DD HH:mm:ss.S AM/PM z
// gwtTimestampFormat = yyyy.MM.dd HH:mm:ss.S a z
DateTimeFormat df = DateTimeFormat.getFormat(gwtTimestampFormat);
String newlyUpdatedTimestampValue = convertToGwtTimeStampFormat(timestampValue);
Date updatedDateTime = df.parse(newlyUpdatedTimestampValue);
String currentTimestamp = convertToGwtTimeStampFormat(currentTimestampValue);
Date currentDateTime = df.parse(currentTimestamp);
boolean isFutureTimestamp = updatedDateTime.after(currentDateTime);
return isFutureTimestamp;
}
private String convertToGwtTimeStampFormat(String gwtTimestampFormat) {
if (gwtTimestampFormat != null && gwtTimestampFormat.length() > 20) { // "2012-12-23 23:12:32.2".length= 21
gwtTimestampFormat = gwtTimestampFormat.replace("-", ".");
gwtTimestampFormat = gwtTimestampFormat.replace("YYYY", "yyyy");
gwtTimestampFormat = gwtTimestampFormat.replace("DD", "dd");
gwtTimestampFormat = gwtTimestampFormat.replace("AM/PM", "a");
return gwtTimestampFormat;
}
else {
return "";
}
}
尝试使用Apache DateUtils。(文档在这里)
使用parseDate方法,因为它支持多种格式,也可以在许多其他地方使用。
例如,在使用parseDate时,您可以将所有这些时间戳模式作为字符串数组提供给以下方法:
public static Date parseDate(String str, String[] parsePatterns) throws ParseException
以接收Date对象。有了这个,你可以像比较数字一样比较日期:
public static int truncatedCompareTo(Date date1, Date date2, int field)
当第一个日期小于、等于或大于第二个日期时,将返回一个负整数、零或正整数。
请注意,这些方法是static
,因此您可以在不需要创建DateUtils
对象的情况下调用这些方法。只需在import
打开罐子后使用DateUtils.yourMethod(yourArgs)
即可。