{"id":1303,"date":"2017-05-06T06:55:58","date_gmt":"2017-05-06T06:55:58","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1303"},"modified":"2024-11-16T14:57:37","modified_gmt":"2024-11-16T14:57:37","slug":"write-a-program-to-convert-date-format-from-one-timezone-to-another","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-program-to-convert-date-format-from-one-timezone-to-another\/","title":{"rendered":"Write a program to convert date format from one Timezone to another?"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>Before writing a simple program to convert date format, we will look at some Java classes which will be useful for the program. The class Date represents a specific instant in time, with millisecond precision. We can use DateFormat class to format and parse date strings.<\/p>\n<h3><strong>DateFormat<\/strong><\/h3>\n<p>DateFormat is an abstract class for date\/time formatting sub classes which formats and parses dates or time in a language-independent manner. The date\/time formatting subclass, such as SimpleDateFormat, allows for formatting (i.e., date \u2192 text), parsing (text \u2192 date), and normalization. The date is represented as a Date object or as the milliseconds since January 1, 1970, 00:00:00 GMT. This is the standard base time known as \u201cthe epoch\u201d.<\/p>\n<p>SimpleDateFormat is a concrete subclass of DateFormat that allows you to choose any user-defined patterns for date-time formatting.<\/p>\n<h3><strong>TimeZone<\/strong><\/h3>\n<p>TimeZone represents a time zone offset, and also figures out daylight savings. Western countries advance the clock reading by one hour ahead during the summer months. In Most cases, you get TimeZone using getDefault which creates a TimeZone based on the time zone where the code is running. For example, for a program running in India , getDefault creates TimeZone object based on Indian Standard Time ( IST )<\/p>\n<p>You can also get TimeZone using getTimeZone along with a time zone ID. For example, the time zone ID for the U.S. Pacific Time zone is \u201cAmerica\/Los_Angeles\u201d.<\/p>\n<p>To get U.S. Pacific Time object, we can use:<\/p>\n<p>TimeZone tz = TimeZone.getTimeZone(\u201cAmerica\/Los_Angeles\u201d);<\/p>\n<p>or<\/p>\n<p>TimeZone tz = TimeZone.getTimeZone(\u201cPST\u201d);<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-562\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/TimeZone-Converter.jpeg\" sizes=\"auto, (max-width: 1343px) 100vw, 1343px\" width=\"1343\" height=\"750\" alt=\"\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3><strong>Sample Code<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import java.util.*;\r\nimport java.text.*;\r\n\r\npublic class TimeZoneConverterExample{\r\n\r\npublic static void main(String args[]){\r\n\r\nDate date = new Date();\r\n Scanner in = new Scanner(System.in);\r\n System.out.println(\"Enter your TimeZone| Sample CST , IST , GMT etc\");\r\n String inputTimeZone = in.next();\r\n System.out.println(\"Enter TimeZone Time &amp; Date to Converted...\");\r\n String outputTimeZone = in.next();\r\n\r\nDateFormat inputFormat = new SimpleDateFormat();\r\n DateFormat outputFormat = new SimpleDateFormat();\r\n\r\nTimeZone inputTime = TimeZone.getTimeZone(inputTimeZone);\r\n TimeZone outputTime = TimeZone.getTimeZone(outputTimeZone);\r\n\r\ninputFormat.setTimeZone(inputTime);\r\n outputFormat.setTimeZone(outputTime);\r\n\r\nSystem.out.println(\"Time in TimeZone \" + inputTimeZone + \" is :\" + inputFormat.format(date));\r\n System.out.println(\"Time in TimeZone \" + outputTimeZone + \" is :\" +outputFormat.format(date));\r\n\r\nin.close();\r\n }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-1303\" data-postid=\"1303\" class=\"themify_builder_content themify_builder_content-1303 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Introduction Before writing a simple program to convert date format, we will look at some Java classes which will be useful for the program. The class Date represents a specific instant in time, with millisecond precision. We can use DateFormat class to format and parse date strings. DateFormat DateFormat is an abstract class for date\/time [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1303","post","type-post","status-publish","format-standard","hentry","category-automation","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1303"}],"version-history":[{"count":3,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1303\/revisions"}],"predecessor-version":[{"id":19633,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1303\/revisions\/19633"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}