0

I have an application that runs the old version of the spring application. The application has the function to create date objects using Date.parse as follows

Date getCstTimeZoneDateNow() {
  String dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
  def zonedDateString = new Date().format(dateFormat, TimeZone.getTimeZone('CST'))
  Date date = Date.parse(dateFormat, zonedDateString)
  return date // Tue Oct 18 20:36:12 EDT 2022 (in Date)
}

However, the code above is deprecated. I need to produce the same result. I read other posts and it seems like Calender or SimpleDateFormatter is preferred. And I thought SimpleDateFormatter has more capabilities.

This post helped me understand more about what is going on in the following code SimpleDateFormat parse loses timezone

Date getCstTimeZoneDateNow() {
  Date now = new Date()
  String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

  SimpleDateFormat sdf = new SimpleDateFormat()
  sdf.setTimeZone(TimeZone.getTimeZone('CST'))

  // cstDateTime prints times in cst
  String cstDateTime = sdf.format(now)  // 2022-10-18T20:36:12.088Z (in String)
  // JVM current time
  Date date = sdf.parse(cstDateTime) // Tue Oct 18 21:36:12 EDT 2022 (in Date)

  return date 
}

Here my goal is to return the date object that is in the format of Tue Oct 18 20:36:12 EDT 2022 The format is good. However, like the post says, when I do sdf.parse(), it prints in JVM time. This means, the format is good but the time zone is off. How can I get the exact same result as before? It does not have to use SimpleDateFormatter. Could be anything.

Thank you so much for reading and for your time.

  • Are you using Java 8+? – tim_yates Oct 19 '22 at 13:49
  • yes, it is java 8 – Gwenda Thomas Oct 19 '22 at 13:55
  • *it seems like `Calender` or `SimpleDateFormatter` is preferred* No, very much the contrary! Those classes are poorly designed and long outdated, the latter in particular notoriously troublesome. In Java or on the JVM you should definitely use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Oct 20 '22 at 20:25
  • I agree with @OleV.V. that it would be a good practice to use `java.time` instead. However, it is always possible to use `java.util.Date` which will work just fine. – MJG Oct 28 '22 at 13:55

1 Answers1

0

Perhaps the important thing is, that the Date is always neutral to the timezone. Given example shows what is to be expected to work from the Java specs:

def format = new SimpleDateFormat()
format.setTimeZone(TimeZone.getTimeZone("CST"))
println new Date()
def date = format.parse(format.format(new Date()))
printf "parsed to %s%n", date
printf "formatted to %s (%s)%n", format.format(date), format.getTimeZone().getDisplayName()

In the output, notice when using the Format and when the toString(), a different time is shown accordingly, which is perfectly fine, since first we format and then parse again in the same format, thus the same time-zone. Later, we use the Date.toString() to output the date, this time using the system default time-zone which is always used when Date.toString() is called. In the output, the time-zone shift is reflected:

Thu Oct 20 09:22:58 EDT 2022
parsed to Thu Oct 20 09:22:00 EDT 2022
formatted to 10/20/22 8:22 AM (Central Standard Time)
MJG
  • 355
  • 1
  • 9
  • Sorry you are right. I did not copy and paste so I had a typo. The timezone was set correctly. That is what I had it in my application – Gwenda Thomas Oct 19 '22 at 14:47
  • I guess even I update the pattern, my problem will not be resolved, am I correct? – Gwenda Thomas Oct 19 '22 at 14:48
  • I still fail to reproduce any problem, so it might be worth to try again and print out some more information if it still gives unexpected results. – MJG Oct 19 '22 at 14:52
  • Yes, I tried it now. It showed me as 11:07. what I want here is 10:07 the format is correct. The timezone is off – Gwenda Thomas Oct 19 '22 at 15:09
  • Hi Please provide the exact code (i.e. complete single executable class file) that you use, along w/ full output generated, otherwise it's just guessing what you could have done wrong. In fact, java.util.Date has no clue about the time-zone. Thanks. – MJG Oct 20 '22 at 06:26
  • Maybe the output is just to be expected, as it depends on the time of year, if there is daylight saving currently on or not, the difference can be explained that it reflects the deviation from EDT to CST which then would be a shift of 1 hour. So the output could be: date at start: Thu Oct 20 02:27:53 EDT 2022 formatted: 10/20/22 1:27 AM (this is CST) parsed: 10/20/22 1:27 AM (this is CST) date returned: Thu Oct 20 02:27:00 EDT 2022 – MJG Oct 20 '22 at 06:30