Rodrigo
Rodrigo

Reputation: 37

Issue with dates on Java Application running on Windows Server

I have a Java Web Application which calls a vbscript. That vbscript takes a .csv file, do some work on the data and produces another .csv (what the vbscript is doing on the data is not important for this question). The issue is that when running it locally everything works fine and the dates are generated correctly as below:

08012021
08022021
08032021
08042021
08052021
08062021
08072021
08082021
08092021
08102021
08112021
08122021
08132021
08142021

But when deployed to the server I am getting the following dates (which is messing up with everything):

01082021
02082021
03082021
04082021
05082021
06082021
07082021
08082021
09082021
10082021
11082021
12082021
08132021
08142021

Could someone please provide any help?

Furthermore I have already changed region settings on the server in order to match my laptop but no success.

Upvotes: 0

Views: 232

Answers (1)

Basil Bourque
Basil Bourque

Reputation: 338326

Expanding on Comment by Avinash

  • Specify a particular formatting pattern when creating your string, rather than relying on the JVM’s current default locale’s format.
  • Always use java.time classes. Never use the flawed legacy date-time classes such as Date, Calendar, and SimpleDateFormat.
  • Consider using standard ISO 8601 formats when exchanging date-time values textually: YYYY-MM-DD

Custom format

To parse strings, define a formatting pattern that matches your expected inputs.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "MMdduuuu" ) ;  // Define a formatting pattern to match your inputs.
LocalDate ld = LocalDate.parse( "08012021" , f ) ;

To generate strings in same format, use the same DateTimeFormatter object.

String outputCustom = ld.format( f ) ;  // If you insist on your custom format, use the same formatter as you used for parsing.

Standard format

Your custom format is particularly ambiguous. I had to read your example data three times to understand your intentions.

I strongly recommend using only ISO 8601 formats for your data exchange. These were designed to be relatively unambiguous, and readable by humans across various cultures. These formats are being rapidly adopted across various industries.

The ISO 8601 formats are used by default in the java.time classes when parsing/generating text. So no need to specify a formatting pattern.

Parsing.

LocalDate ld = LocalDate.parse( "2021-08-01" ) ;  // Parsing ISO 8601 standard format.

Generating.

String outputIso8601 = ld.toString() ;  // Generating ISO 8601 standard format.

Upvotes: 1

Related Questions