1

I'm trying to write a Java script that computes the time difference between the current date and the last updated time stored on our Parse backend. Can anyone help me spot the bug in my code? You'd think this isn't so bad, but I've looked on Stack Overflow for hours to no avail.

//Create a date formatter.
        SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'LLL'Z'");
//Create a date object for today's date.
        Date currentDate=new Date();
//Create a string for the date from parse.
        String parseTime = singleClaim.getString("updatedAt");
//Create a string for the current date.
        String currentTime=currentDate.toString();
//Initialize the date object for the updatedAt time on Parse.
        Date parseDate = null;
//Initialize the date object for the current time.
        Date FormattedCurrentDate = null;
        try {
//Here, we convert the parseTime string into a date object and format it.
            parseDate = formatter.parse(parseTime);
//Here, we convert the currentTime string into a date object and format it.
            FormattedCurrentDate = formatter.parse(currentTime);
} 

        catch (Exception e) {
            e.printStackTrace();
        }           

   //Get the time difference from the current date versus the date on Parse.
        long difference = FormattedCurrentDate.getTime()-parseDate.getTime();
Filburt
  • 17,626
  • 12
  • 64
  • 115
MarisolFigueroa
  • 757
  • 1
  • 14
  • 31

2 Answers2

2

The combination of these two lines is probably causing your issue:

String currentTime=currentDate.toString();
// ...
FormattedCurrentDate = formatter.parse(currentTime);

The currentTime variable does not contain a properly formatting string that can be comsumed by your formatter.

I also see no need to create such a string, you could just do it as follows:

long difference = currentDate.getTime() - parseDate.getTime();

If you absolutely insist on making a round trip from date to string and back, you would have to create your currentTime string as follows:

currentTime = formatter.format(currentDate);
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • but doesn't currentDate need to be formatted according to the formatter to be able to subtract parseDate from it? – MarisolFigueroa Jul 31 '15 at 07:04
  • 1
    @Aly_G In Java a `Date` is just a container for the number of milliseconds since the Unix Epoch, it has not internal format of it's own.. – MadProgrammer Jul 31 '15 at 07:06
0

You should not call toString() to convert Date to String. And why you convert currentTime to String and later parse it to a date? This makes no sence.

//Create a date formatter.
        SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'LLL'Z'");
//Create a date object for today's date.
        Date currentDate=new Date();
//Create a string for the date from parse.
        String parseTime = singleClaim.getString("updatedAt");
//Create a string for the current date.
        String currentTime=currentDate.toString();
//Initialize the date object for the updatedAt time on Parse.
        Date parseDate = null;
        try {
//Here, we convert the parseTime string into a date object and format it.
            parseDate = formatter.parse(parseTime);
} 

        catch (Exception e) {
            e.printStackTrace();
        }           

   //Get the time difference from the current date versus the date on Parse.
        long difference = currentDate.getTime()-parseDate.getTime();
Jens
  • 67,715
  • 15
  • 98
  • 113