0

somebody please tell me how to get the time from the TimeZone class. When I run this code

System.out.println("Default Timezone: " + TimeZone.getDefault())

I got this

Default Timezone: sun.util.calendar.ZoneInfo[id="Asia/Manila",offset=28800000,dstSavings=0,useDaylight=false,transitions=10,lastRule=null]

I can't find any function that will get the time such as TimeZone.getDefault().getTime(). Please help.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
TheOnlyIdiot
  • 1,182
  • 7
  • 17
  • 37
  • Question is not clear. Are you asking for the current time-of-day for a particular time zone? Or are you asking for information about a time zone itself, its offset from UTC? – Basil Bourque Feb 23 '18 at 00:34

7 Answers7

4

Try below to get the time for EST TimeZone:

TimeZone est = TimeZone.getTimeZone("America/New_York");
Calendar calendar = new GregorianCalendar(est);
System.out.println(calendar.getTime());     //<-prints the date
System.out.println(calendar.getTimeInMillis()); //<-prints the time in milliseconds

You can change timezone to other timezones e.g. PST to get the time in other timezones:

TimeZone pst = TimeZone.getTimeZone("America/Los_Angeles");
calendar.setTimeZone(pst);
System.out.println(calendar.getTime());      //<-prints the date
System.out.println(calendar.getTimeInMillis()); //<-prints the time in milliseconds

Hope this helps.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • Negative Voter: Please leave a comment to help understand, what's wrong? – Yogendra Singh Oct 15 '12 at 03:56
  • Both Calendar.getTime() and Calendar.getTimeInMillis() return values representing the instant in time within the calendar, which is independent of both time zone and calendar system. http://stackoverflow.com/a/9867266/2112688 – Kiryl Bielašeŭski May 13 '16 at 13:32
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), `TimeZone`, and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Feb 23 '18 at 00:32
3
Date theCurrentDateAndTime = new GregorianCalendar(timeZone).getTime();
Mark Lutton
  • 6,959
  • 7
  • 41
  • 57
2

TimeZone is a abstract class which represent timezone not time. As you mentioned you are invoking the getDefault(),TimeZone.getDefault() by using getDefault() you will get the timezone based on where the program is running.

If you want to just print the date, then you have options like Calendar or Date or if you wish to move with timezone specific time then set the timezone and get the time of that zone.

Your program will print the date in this way(this is not only the way):

    TimeZone defaultTimezone = TimeZone.getDefault();
    Calendar calendar = new GregorianCalendar(defaultTimezone);
    System.out.println(calendar.getTime());
subodh
  • 6,136
  • 12
  • 51
  • 73
1

The TimeZone class represents a time zone not the time. You will have to use either the Date or the Calendar class instead for the time.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
1

You need to use either Date (or) Calendar API to get today date/time.

These APIs use default time zone configured in your system.

kosa
  • 65,990
  • 13
  • 130
  • 167
1

You don't.

Javadoc for TimeZone. You'll note this has nothing to do with the current time.

See Calendar

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
0

tl;dr

LocalTime.now(                    // Capture the current time-of-day for a particular time zone. Result discards the zone, leaving an object unaware of any zone or offset.
    ZoneId.of( "Asia/Manila" )    // Represent the time zone, the history of past, present, and future changes in offset for a specific region.
)

23:45

java.time

The modern approach to date-time handling uses the java.time classes that supplanted the troublesome old legacy date-time classes.

Your Question is unclear? Are you asking for the current time-of-day for a particular time zone? Or are you asking for information about a time zone itself, its offset from UTC?

Zones

An offset-from-UTC is a number of hours, minutes, and seconds displaced from the same moment UTC. A time zone is a history of the past, present, and future changes in offset used by the people of a particular region.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "Asia/Manila" ) ; 

Current time-of-day

For only the current time-of-day as seen on the wall-clocks by the people of a particular region, use LocalTime.now and pass the desired zone. The resulting object lacks any concept of zone or offset as the passed zone is discarded after determining the current moment.

LocalTime lt = LocalTime.now( z ) ;

23:45

For the date and time-of-day in that zone, use ZonedDateTime.

ZonedDateTime zdt = ZonedDateTime.now( z ) ;

To capture the current moment in UTC, use Instant.

Instant instant = Instant.now( z ) ;

Time zone info

The ZoneId and ZoneOffset classes supplant TimeZone.

ZoneRules rules = z.getRules() ;

You can interrogate a ZoneId about the rules it uses to define the behavior a particular time zone. You must pass a moment (a Instant), as the entire point of a time zone is that the offset used by the people of that region has changed over history. For example, countries silly enough to practice Daylight Saving Time (DST) change their offset twice a year.

ZoneOffset offset = rules.getOffset( zdt.toInstant() ) ;

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 the java.time classes.

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

Using a JDBC driver compliant with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings nor java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154