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.

  1. static TimeZone getDefault() - Provides default time zone. It gets the time zone by System date. 
  2. 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 :


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 :


Output of Java program how to find time zone information.
Output - How to find time zone information in Java?

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.

About Author:

I am simple guy with lot of ambitions. My main motive is to share whatever knowledge I have related to programming. With me you can easily learn how to solve any programming problem in Java.You can connect with me on social networking sites also.


Let's Get Connected: Linkedin | Facebook |

How to get Time Zone information in Java? How to get Time Zone information in Java? Reviewed by Rohit Agarwal on 11/29/2016 Rating: 5

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.

Powered by Blogger.