Question

I have few timestamp format

time stamp format contains YYYY MM DD hh mm ss S AM/PM CountryName/CityName(zone)

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 

I want to do validation on timestamp value, If time stamp value is future value (or greater than existing) then want to show.notification message to user.

For Date format I have written following code which works to check Future date

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
        }

EDIT:

Following code only works for timestamp format = 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
                }

What changes I need to do for all other time-stamp format ?

Thanks in advance.

Was it helpful?

Solution

This is my solution and It Works :)

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

OTHER TIPS

Try using Apache DateUtils. (doc is here)

Use parseDate method(s) as it supports a wide variety of formats and can be possibly used in many other places too.

For example while using parseDate you can feed all these time stamp patterns as a string array to the following method:

public static Date parseDate(String str, String[] parsePatterns) throws ParseException

to receive Date objects. And with this you can compare dates just like you compare numbers:

public static int truncatedCompareTo(Date date1, Date date2, int field)

This returns you a negative integer, zero, or a positive integer as the first date is less than, equal to, or greater than the second.

Notice that these methods are static so you can invoke these without needing to create a DateUtils objects. Just use DateUtils.yourMethod(yourArgs) once you have imported the jar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top