如何在GWT中找出将来的时间戳值?

堆栈溢出

我的时间戳格式很少

时间戳格式包含YYYY MM DD hh mm ss S AM / PM CountryName / CityName(区域)

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 

我想对时间戳值进行验证,如果时间戳值是将来值(或大于现有值),则想向用户显示show.notification消息。

对于日期格式,我编写了以下代码来检查将来的日期

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 "";
        }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在java中给出时间戳值作为参数?

来自分类Dev

如何获取Twig中的时间戳值?

来自分类Dev

如何获取Twig中的时间戳值?

来自分类Dev

如何在Firebase中验证时间戳?

来自分类Dev

如何在js中验证时间戳

来自分类Dev

如何在python中获取日期时间对象列表并找出最小值

来自分类Dev

如何在SQLite查询中填写/插值错误时间戳?

来自分类Dev

Python如何在MongoDB的ISODate类型中插入时间戳值?

来自分类Dev

如何在 SQL-Server 的表中管理自动插入时间戳值?

来自分类Dev

如何从Oracle时间戳值中仅提取时间分量?

来自分类Dev

如何在Orion中存储时间戳或日期时间?

来自分类Dev

如何在MySQL时间戳中设置时间?

来自分类Dev

如何在Orion中存储时间戳或日期时间?

来自分类Dev

如何在MongoDb中保存时间戳类型值 爪哇

来自分类Dev

如何在R中的时间戳中处理时区?

来自分类Dev

如何从值中获取开始日期时间戳和结束日期时间戳

来自分类Dev

如何在 SQL Server 中的时间戳和时间戳之间

来自分类Dev

如何迭代比较数组值中的时间戳

来自分类Dev

如何从包含时间戳的日志文件中获取日期值

来自分类Dev

如何从javascript填充firestore文档中的时间戳类型值?

来自分类Dev

如何在Hive中添加时间戳列

来自分类Dev

如何在Firebase中获取Unix时间戳?

来自分类Dev

如何在UTC时区中设置Rails日志时间戳?

来自分类Dev

如何在Hive表中插入时间戳?

来自分类Dev

如何在Oracle SQL中获取日期列的时间戳?

来自分类Dev

如何在目录中读取文件时间戳

来自分类Dev

如何在PHP中获取UTC时间戳

来自分类Dev

如何在openCV中基于时间戳选择特定帧?

来自分类Dev

如何在ClojureScript中获得时间戳?

Related 相关文章

热门标签

归档