0

How to convert DateTime to Local Date Format?

Example: date: 1/25/2014 12:00:00 AM This date is US format but in my machine I use TR format 25/1/2014 and also assume that another machine use another format Example: 2014/1/25

How can I convert this date to local date format programmaticaly?

I am using java version 1.7 and i want to use java.util.Calendar

Thanks in advance.

HurkanSeyhan
  • 374
  • 2
  • 3
  • 14
  • you mean from 1/25/2014 to 25/1/2014? – Rod_Algonquin Jun 14 '14 at 21:04
  • 1
    Which java version?? pre 1.8 ? – isaias-b Jun 14 '14 at 21:06
  • It's not obvious what you are asking about. Joda-Time, the Java 8 date API or something completely different. Please clarify your question; preferably with a simple code example that illustrates your issue. – Boris the Spider Jun 14 '14 at 21:09
  • @isi yes i am using 1.7 – HurkanSeyhan Jun 14 '14 at 21:09
  • One more thing will be necessary, which of the java `Date` class do you mean? `java.util.Date` or `java.sql.Date` – isaias-b Jun 14 '14 at 21:14
  • your question is possibly a duplicate to the question where UTC dates are converted into local dates in within java [see here](http://stackoverflow.com/questions/13307724/local-date-time-to-utc-and-then-utc-to-local-date-time) – isaias-b Jun 14 '14 at 21:16
  • 2
    My advice to you, would be to **not use the calendar at all** if possible switch to java 1.8 or add a library called joda.time which is merged into java 1.8 as the new standard. pre 1.8 really easy things were difficult to achive, take a look at [JSR310](https://jcp.org/en/jsr/detail?id=310) – isaias-b Jun 14 '14 at 21:19
  • To display a java.util.Date using the current machine's local date format, pass the Date object to the [format](http://docs.oracle.com/javase/8/docs/api/java/text/DateFormat.html#format-java.util.Date-) method of `DateFormat.getDateInstance(DateFormat.SHORT)`. – VGR Jun 14 '14 at 21:36

1 Answers1

3

DateTimeFormatter.ofLocalizedDateTime

Use it to obtain a locale-specific date format for the ISO chronology.

Demo:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtfLocalized = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.SHORT);
        // Test
        LocalDateTime date = LocalDateTime.now();

        System.out.println(dtfLocalized.withLocale(Locale.US).format(date));
        System.out.println(dtfLocalized.withLocale(Locale.UK).format(date));
        System.out.println(dtfLocalized.withLocale(Locale.CHINESE).format(date));
        System.out.println(dtfLocalized.withLocale(Locale.GERMAN).format(date));
        System.out.println(dtfLocalized.withLocale(Locale.forLanguageTag("tr")).format(date));
        System.out.println(dtfLocalized.withLocale(Locale.getDefault()).format(date));
    }
}

Output:

5/8/21, 6:54 PM
08/05/2021, 18:54
2021/5/8 下午6:54
08.05.21, 18:54
8.05.2021 18:54
08/05/2021, 18:54

Note: If you want to format just the date part, replace DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.SHORT) with DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).

Learn more about the the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 1
    The question is not clear, but the input appears to be a string with a date plus a time-of-day: `1/25/2014 12:00:00 AM`. Your Answer addresses only the date. I suspect this Question is better off closed for being unclear. – Basil Bourque May 08 '21 at 17:29
  • I suspect the Question is about parsing the input string `1/25/2014 12:00:00 AM`, then extracting the date-only portion. – Basil Bourque May 08 '21 at 17:43
  • 1
    @BasilBourque - I think it's about formatting. The requirement stated by the OP is: *How to convert DateTime to Local Date Format?...* – Arvind Kumar Avinash May 08 '21 at 17:45
  • 1
    I wonder if “TR format” refers to the [country code `TR`](https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes) for Turkey? The author’s profile page does mention Turkey . – Basil Bourque May 08 '21 at 17:46
  • 1
    @BasilBourque Most probably. The OP’s profile informs us of a location in Turkey. – Ole V.V. May 08 '21 at 17:48
  • 1
    For my part I read the question as how to format the date part ignoring the time part. And for my part I think I’d add `Locale.forLanguageTag("tr")` and `Locale.getDefault()` Up to you, of course. – Ole V.V. May 08 '21 at 17:53
  • 1
    @OleV.V. - Thanks. I have added the Turkish locale as well as the default locale now. I've also added a note regarding the formatting of just the date part. – Arvind Kumar Avinash May 08 '21 at 18:02