0

I have Rest API which will accept date time in URI it works fine except for UTC.

I have the following code which will format the DateTime and outputs the result, when I use CDT, EST, PDT,PST etc, but I want the date format to also accept UTC time zone along with all other. Client can send the date time in any format(UTC mostly, but also includes CDT,EST) and I need to pass it as is, it is client's choice what time zone to pass

Date now = new Date();

SimpleDateFormat inStr = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmz")

This works for CDT, EST,PDT but not for UTC I want to include UTC aswell in the rest API call get the desired output.

Can some one please help me on this

Valid values are - 2018-07-11T20:30UTC , 2018-07-11T18:30CDT

  • 1
    I don't like the idea of storing different timezones in your database. I think you should store all timestamps in a single timezone, and usually UTC is the one used. This way, it is the client's responsibility to worry about how to render a given timestamp (and the client would know it's own timezone, whereas the server might not). – Tim Biegeleisen Jul 12 '18 at 02:41
  • @Tim Biegeleisen - i am not storing the time zones in DB, I need to parse(UTC, CDT etc) what ever the client sends to me – SpringFramework Jul 12 '18 at 02:45
  • As of now the CDT, EST all other time zones work, but UTC doesn't. – SpringFramework Jul 12 '18 at 02:45
  • Possible duplicate of [Get Date Object In UTC format in Java](https://stackoverflow.com/questions/22091107/get-date-object-in-utc-format-in-java) – Tim Biegeleisen Jul 12 '18 at 02:46
  • @Tim Biegeleisen - Thanks Tim. But this will not work, I cannot explicitly set the time, it comes from client, it might be UTC or CDT or any other. I need to pass all time zones – SpringFramework Jul 12 '18 at 02:53
  • How are you parsing the incoming timestamp? Note that a Java `Date` has no timezone, it is just a point in time. – Tim Biegeleisen Jul 12 '18 at 02:56
  • Im using the following code to pass the incoming timestamp Date now = new Date(); SimpleDateFormat inStr = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmz") The above code pass time zones CDT, PST etc but not UTC – SpringFramework Jul 12 '18 at 02:57
  • 1
    Your code works fine for me, and accepts UTC. Are you sure you haven't made a mistake? – Dawood ibn Kareem Jul 12 '18 at 03:01
  • @Dawood - yea it is not working for me with UTC. Can you please share how did you test ? – SpringFramework Jul 12 '18 at 03:09
  • `SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmz"); Date test = format.parse("2018-06-14T12:34UTC"); System.out.println(test);` – Dawood ibn Kareem Jul 12 '18 at 03:10
  • @DawoodibnKareem - Actually I just need to pass the date time argument in the rest API URI. it could be CDT or UTC or any thing. it will be in the following format 2018-07-11T20:30UTC , 2018-07-11T18:30CDT UTC is not working with SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmz") rest all works – SpringFramework Jul 12 '18 at 03:17
  • OK, what comes in from your REST API is a different question entirely. Use your debugger to check what the actual date string is. If it's something like `"2018-06-14T12:34UTC"`, then your code works. – Dawood ibn Kareem Jul 12 '18 at 03:18
  • okay let me check. – SpringFramework Jul 12 '18 at 03:23
  • Which Java version are you using? Could you [create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve), please? – Ole V.V. Jul 12 '18 at 07:48
  • Relying on three letter time zone abbreviations seems dangerous to me. They are ambiguous, so I wouldn’t be too sure what you get. Also I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 12 '18 at 07:49
  • I can neither reproduce on Java 7 nor on Java 10. – Ole V.V. Jul 12 '18 at 07:53

0 Answers0