-4

I need to format date time in java for provided time zone. E.g. mm/dd/yyyy for US and dd/mm/yyyy for DE. I have time zone and i can get zoneId, can someone help me. Many thanks.

  • 2
    Try googling your title. – Guy Jun 06 '17 at 08:11
  • Is going to be hard to help you if you don't provide a [sscce](http://sscce.org/) with the code you have tried!!... – ΦXocę 웃 Пepeúpa ツ Jun 06 '17 at 08:13
  • Go look here. https://stackoverflow.com/questions/7670355/convert-date-time-for-given-timezone-java – fluffy Jun 06 '17 at 08:13
  • look here too [Link]https://stackoverflow.com/questions/19112357/java-simpledateformatyyyy-mm-ddthhmmssz-gives-timezone-as-ist – Pradeep Singh Jun 06 '17 at 08:15
  • I think people here misunderstand the question. It's not about displaying the time in a given time zone. It's about changing the date format based on the custom in the country, deducing the country from the time zone. – RealSkeptic Jun 06 '17 at 08:41
  • `DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(Locale.US)`. Similarly with `Locale.GERMANY` etc. – Ole V.V. Jun 06 '17 at 08:52

2 Answers2

1

Use the modern Java date and time classes for everything that has got to do with dates or times.

    DateTimeFormatter usFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
            .withLocale(Locale.US);
    System.out.println(date.format(usFormatter));
    DateTimeFormatter deFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
            .withLocale(Locale.GERMANY);
    System.out.println(date.format(deFormatter));

This will print something like

6/27/17
27.06.17

It’s not exactly the formats you asked for, but it’s the formats Java thinks are appropriate for those two locales. I’d try them, and if the users complaint, build my own DateTimeFormatter from a pattern string (like MM/dd/uuuu, for example).

I used a LocalDate for the date in the code, but the same code should work with a LocalDateTime, OffsetDateTime or ZonedDateTime.

If you meant to deduce the locale from the time zone in a ZonedDateTime, I don’t think you can do that reliably. There are often many countries in a time zone, each country having its own locale and its own way of formatting dates. Germany, for example, shares its time zone with Norway, Sweden, Denmark, Poland, France, Switzerland, Austria and Italy, Spain, Serbia and many others.

And if you meant to deduce it from the time zone in an oldfashioned Date object, you certainly cannot simply because a Date does not hold a time zone in it.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • That's what I need, I have date in Long, after I set default time zone, I create new Date(Long ticks), but I need to format it for explicit country. Your answer is good for it if you have Locale, but I have only time zone. –  Jun 06 '17 at 10:32
  • You may be able to make some guesses. I figure people in the mentioned countries will be able to read a date formatted with Germany locale even though they may not all think of it as their own. – Ole V.V. Jun 06 '17 at 10:42
  • To convert a `long` containing milliseconds since the epoch to something that can be formatted using the above code, use `Instant.ofEpochMilli(millisSinceEpoch).atZone(ZoneId.of("Europe/Berlin"))` — please fill in your user’s time zone instead of Berlin time zone. This gives you a `ZonedDateTime`, and as I said in the answer, my code should work with it. – Ole V.V. Jun 06 '17 at 10:45
  • You may want to investigate a little further whether there may either be a way to obtain the user’s locale or a way to have the client format the date. See for example http://www.jguru.com/faq/view.jsp?EID=12253. – Ole V.V. Jun 06 '17 at 10:50
  • 1
    I'll definitely take a look at this one article. Thank you a lot! –  Jun 06 '17 at 10:53
0

Hope this will help you,

import java.util.*;
import java.text.*;

public class DateDemo {

   public static void main(String args[]) {

        Date date1 = new Date();
        System.out.println(date1);

        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
        Date date2 = new Date();
        System.out.println(date2);

        TimeZone.setDefault(TimeZone.getTimeZone("EST"));
        Date date3 = new Date();
        System.out.println(date3);

   }
}

Output:

Tue Jun 06 14:02:51 IST 2017
Tue Jun 06 08:32:51 UTC 2017
Tue Jun 06 03:32:51 EST 2017
AnjuT
  • 166
  • 1
  • 4
  • 17
  • 1
    I think you didn't understand the question. It was about displaying in different formats based on the time zone. In some countries the date format is mm-dd-yyyy, in some countries it is dd/mm/yyyy, etc.. – RealSkeptic Jun 06 '17 at 08:40