The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to get current Date Timestamps in Java on GMT or Local Timezone Example

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Javin Paul

Posts: 1090
Nickname: javinpaul
Registered: Jan, 2012

Javin Paul is Java Programmer working on Finance domain.
How to get current Date Timestamps in Java on GMT or Local Timezone Example Posted: Jan 12, 2012 7:41 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Javin Paul.
Original Post: How to get current Date Timestamps in Java on GMT or Local Timezone Example
Feed Title: Javarevisited Blog
Feed URL: http://javarevisited.blogspot.com/feeds/posts/default?alt=rss
Feed Description: Blog on Java, Unix, Linux, FIX Protocol technology. I share my experience as tutorial, article, interview questions, programming, design etc.
Latest Java Buzz Posts
Latest Java Buzz Posts by Javin Paul
Latest Posts From Javarevisited Blog

Advertisement



Getting current data and timestamps in java is requirement for most of java enterprise application. Formatting date in java or parsing date in Java is such a common functionality that you will need it now and than. Though most of programmer look Google for finding current date and timestamps code in Java, its worth to having an idea how you can do that by using java.util.Date class. some time you might need formatted data string say "yyyy-MM--dd:HH:mm:ss". That is also possible by using dateformat in Java. I have touched base on string to date conversion and now in this article we will see that in detail in terms of current date, time-stamp and timezone values. if you are working in global core java application which runs on different timezones you must need to include timezone in your date and timestamp to avoid any confusion.


Find Current Date Timestamps in Java Timezone Example

How to get current data and timestamp in GMT timezone

find current date timestamp on different timezone in java exampleGMT timezone is sort of standard timezone for recording date and timestamp. if your application is not using any local timezone that you can use GMT for storing values on server. here is quick example of getting current date and timeStamp value in GMT timezone in Java.

Example of Current Date TimeStamp in Java SimpleDateFormat


SimpleDateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
gmtDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

//Current Date Time in GMT
System.out.println("Current Date and Time in GMT time zone: " + gmtDateFormat.format(new Date()));

Output:
Current Date and Time in GMT timezone: 2012-01-10 07:02:59


Code Example of current date and timestamp in GMT

Current timestamp and date in local time zone GMT is not always everybody require some time you need that in your local timezone or any other time zone. what you need to do is reset timezone of date formatter to your local timezone. here is another quick example of getting current date and timestamp values in local timezone:

SimpleDateFormat Example current Date Timestamps in Java

  
//Current Date time in Local time zone
SimpleDateFormat localDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//Current Date Time in Local Timezone
System.out.println("Current Date and Time in local timezone: " + localDateFormat.format( new Date()));

Output:
Current Date and Time in local timezone: 2012-01-10 02:32:59


That’s  All on this quick tip of getting current date and timestamp value on different timezones in Java. You just need to be bit careful because DateFormat and SimpleDateFormat are not Thread-safe in Java and should not be shared between multiple threads, otherwise it can create very subtle issues like incorrect Date.

Some other Quick tips on Java which you may like

Read: How to get current Date Timestamps in Java on GMT or Local Timezone Example

Topic: Java EE 6 vs. Spring Framework: A technology decision making process Previous Topic   Next Topic Topic: Domain modeling with Spring Data Neo4j

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use