(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?