0

I created a rest service to return a date.

I use Java on the back end and AngularJS on the front end.

My code in java is something like this:

   SimpleDateFormat myFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   Date myDate = myFormat.parse("1900-01-01 12:00:00");
   return myDate;

And when I get this value from the service I display it on the front end with this code:

 <div>
    <h4>{{myDate | date:'yyyy-MM-dd HH:mm:ss'}}</h4>
 </div>

The page shows "1899-12-31 23:56:16".

I tried again but with the current date and it shows normally in the system.

4
  • 1
    Smells like a timezone representation problem. Which timezones are the client and the server in? Commented Aug 24, 2016 at 17:18
  • The both timezones are GMT:-5, but the error no happen always, sometimes show a correct hour Commented Aug 24, 2016 at 18:36
  • This is your back-end? Skip the HTML/AngularJS part for now, and trace through the Java code. This Question is not appropriate to Stack Overflow, with not enough information and no specific programming question. Commented Aug 24, 2016 at 19:27
  • What is the information that you need? i wrote the problem, my problem is a put a date in java and after i get a different date in angular, the question is: Why i get a different date? or Why this date has a different interpretation? Commented Aug 24, 2016 at 20:54

1 Answer 1

1

I have tested with all the january 1st dates from 1900 to 2020, and, in fact, Java and javascript apparently return different milliseconds values in certain intervals (in fact, only the dates between 1912-1940 coincide).

I've tested empirically that this somehow random difference is caused by the lack of an specific Time Zone when parsing the date string in Java (in the javascript part, the reference used is implicitly UTC, I guess). Moreover, it is always a good practice to specify a TimeZone when parsing dates.

So I recommend you add the timezone specifier for UTC (or GMT+0000) in your java program:

    SimpleDateFormat myFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
    Date myDate=myFormat.parse("1900-01-01 12:00:00 +0000");

I insist that, in my guess, the AngularJS scriptlet is referencing from UTC, no matter which time zone the client is actually in.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.