0

I wrote a code where Calender was prepared according to time zone.

 Calendar toDayDateCalendar = Calendar.getInstance(TimeZone.getTimeZone(appTheme.getTimezoneId()));

Date formattedSelectedDate = toDayDateCalendar.getTime();

But I am getting the "formattedSelectedDate" according to System.i want to get the date according to timezone.How can i get it.

user3692033
  • 585
  • 3
  • 8
  • 21
  • 1
    I recommend you don’t use `Calendar` and `Date`. Those classes are poorly designed and long outdated. Instead use `ZonedDateTime` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 30 '21 at 16:52

1 Answers1

2

(A) A java.util.Date object, despite the name, represents a date and a time-of-day as seen in UTC. A Date does not have a time zone. Well, actually it does down deep, but for our purpose here that is irrelevant.

So your expectation of "i want to get the date according to timezone" makes no sense.

(B) You are using terrible date-time classes that were years ago supplanted by the modern java.time classes. Never use Calendar, never use Date, never use TimeZone.

Get the current moment as seen in a particular time zone.

ZoneId zoneId = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime nowTunis = ZonedDateTime.now( zoneId ) ;

If you want the date-only, without a time-of-day and without a time zone, use LocalDate.

ZoneId zoneId = ZoneId.of( "Asia/Tokyo" ) ;
LocalDate todayTokyo = LocalDate.now( zoneId ) ;

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.

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

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

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • If you want the date-only, without a time-of-day and without a time zone, use LocalDate.What did you mean "without a time zone". – user3692033 Apr 30 '21 at 06:29
  • @user3692033 A `LocalDate` has simply a year, a month, and a day-of-month. But no time zone. For example, “January 23, 2021”, `LocalDate.of( 2021 , Month.JANUARY , 23 )`. We do not know if that means the 23rd in Tokyo Japan or the 23rd in Toledo Ohio US — two dates that happen at very different times, several hours difference. For any given moment, the date varies around the globe by time zone. The day in Japan starts and ends much earlier than in the US mainland. Additionally, the day can vary in length if we account for time zone, because of anomalies such as Daylight Saving Time (DST). – Basil Bourque Apr 30 '21 at 06:33
  • can we set different timezone to a LocalDate? – user3692033 Apr 30 '21 at 08:45
  • No, @user3692033, a `LocalDate` hasn’t got a time zone, so that would not make sense. Why would you have wanted to do that? Asking because there surely is a way to obtain your end goal with java.time. – Ole V.V. Apr 30 '21 at 18:43