-2

Is there anyway to force the computer to change its date format by code using java? My app stores the date in a string using a specified format and in a file on disk. The format chosen affects only the string variable but not the file as its forced to use the system regional settings. I tried to set the Locale or change the default locale in SimpleDateFormat() but it didn't go. I don't understand why the system settings force a String to follow a date pattern. Any clarification would be appreciated.

  • 2
    Show us your code. But before that, see [how to complete a Minimal, Complete, and Verifiable Example](/help/mcve). – MD XF Dec 04 '16 at 05:27
  • This Question makes no sense. Provide much more detail, and example code. If you you are writing a date-time value as text to a file, that should be easy to demonstrate in a code snippet extracted from your app. – Basil Bourque Dec 04 '16 at 06:02
  • 1
    Welcome to Stack Overflow! Please review our [SO Question Checklist](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) to help you to ask a good question, and thus get a good answer. – Joe C Dec 04 '16 at 07:44

1 Answers1

1

Your Question is not at all clear. No such thing as "System date format".

Avoid legacy date-time classes

The old date-time classes bundled with the earliest versions of Java have proven to be troublesome, confusing, and flawed. Avoid java.util.Date, .Calendar, and SimpleDateFormat. These are now legacy, supplanted by the java.time classes.

ISO 8601

If you are serializing a date-time value to disk in a file as text, use the standard ISO 8601 formats. These formats are intuitive across cultures, unambiguous, and growing in popularity including in new internet and information standards.

The java.time classes use ISO 8601 formats by default when parsing/generating strings of date-time values.

Get the current moment in UTC with a resolution of up to nanoseconds using the Instant class.

Instant instant = Instant.now();

Generate a string to represent that moment as text in standard ISO 8601 format.

String output = instant.toString();

2016-12-04T06:12:13.455Z

Easy to parse back into an object again.

Instant instant = Instant.parse( "2016-12-04T06:12:13.455Z" );

Usually best to work in UTC, but you can adjust that moment into a time zone if desired.

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );
String outputZdt = zdt.toString();

2016-12-04T01:12:13.455-05:00[America/Montreal]

See live code at IdeOne.com.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8 and SE 9 and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thank you very much Basil for your help. You made it clear, I need a lot more of reading and practice.I meant by System Date Format the settings in control panel -> region where the user set the date and time format for the OS. That format seems to override everything outside the JVM environment like the file I set on the system. I'm sorry I didn't reply earlier as I didn't get an email notification of your comment. – Ibrahim Al Ouayed Dec 04 '16 at 20:45