How to get Time Zone information in Java?
Problem Description :
Write a program in Java that prints Time Zone information of current date.
Concept :
A time zone is a region where the same standard time is used. In java for getting time zone we have following 2 methods in TimeZone class that is present in java.util.* package.
- static TimeZone getDefault() - Provides default time zone. It gets the time zone by System date.
- static TimeZone getTimeZone(String id) - Provides time zone based on ID. For example - if ID is "Europe/London" then it returns time zone in GMT.
Recommended :
Java Program :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javamultiplex.datetime; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.TimeZone; | |
/** | |
* | |
* @author Rohit Agarwal | |
* @category Date and Time | |
* @problem How to get time zone information? | |
* | |
*/ | |
public class TimeZoneInformation { | |
public static void main(String[] args) { | |
// Get Current date. | |
Date date = new Date(); | |
// Get Default Time Zone. | |
TimeZone time = TimeZone.getDefault(); | |
System.out.println("ID : " + time.getID()); | |
System.out.println("TimeZone name :" + time.getDisplayName()); | |
// z - General timezone | |
SimpleDateFormat format = new SimpleDateFormat("z"); | |
// Convert Date to String. | |
String timezone = format.format(date); | |
System.out.println("Timezone : " + timezone); | |
} | |
} |
Output :
References :
Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.
Tags : Date and Time problems, Timezone information in Java, Solution in Java, String, SimpleDateFormat.
How to get Time Zone information in Java?
Reviewed by Rohit Agarwal
on
11/29/2016
Rating:

No comments:
Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.