Solved

Get System time

Posted on 2014-03-19
12
303 Views
Last Modified: 2014-03-20
Hi,

I want the current system time but the below code return the GMT time. However the date displayed is correct only the time is in GMT?

	Calendar calendar = Calendar.getInstance();
		String date = new SimpleDateFormat("yyyyMMddHHmmss")
				.format(calendar.getTime());
		System.out.println("date" + date);

Open in new window

0
Comment
Question by:Rocking
  • 5
  • 4
  • 3
12 Comments
 
LVL 27

Expert Comment

by:rrz
ID: 39941340
Where are you?  I am in California.  Try using  
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"));

Open in new window

0
 
LVL 12

Expert Comment

by:KRUNAL TAILOR
ID: 39941351
Hi him121,

Please try one of this will solve your problem.

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));

or

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));

Thanks & Regards,
Krunal T. Tailor
0
 

Author Comment

by:Rocking
ID: 39941355
i tried with the both above code but result is same i.e gmt time

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta"));

is there any system setting which need to be set?
My current system time zone is "UTC+5:30"
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 12

Expert Comment

by:KRUNAL TAILOR
ID: 39941364
Hi him21,

prefer using the Calendar object like below.

Calendar now = GregorianCalendar.getInstance()
You can also get a Date object from the Calendar. More reference below link.

http://java.sun.com/javase/6/docs/api/java/util/GregorianCalendar.html

Thanks & Regards,
Krunal T. Tailor
0
 

Author Comment

by:Rocking
ID: 39941370
i have earlier tried with this also,but no luck

Calendar now = GregorianCalendar.getInstance()
0
 
LVL 12

Expert Comment

by:KRUNAL TAILOR
ID: 39941372
Hi him21,

Have try this code if not work then some setting will come on physical m/c,

    DateFormat utcFormat = new SimpleDateFormat(patternString);
    utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    DateFormat indianFormat = new SimpleDateFormat(patternString);
    utcFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    Date timestamp = utcFormat.parse(inputString);
    String output = indianFormat.format(timestamp);

Thanks & Regards,
Krunal T. Tailor
0
 

Author Comment

by:Rocking
ID: 39941377
Same Result,all these code i have tried before also:)

            String patternString = "yyyyMMddhhmmss";
            DateFormat utcFormat = new SimpleDateFormat(patternString);
            utcFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
               
      System.out.println(+utcFormat.getTimeZone() +"::"+utcFormat.format(new Date()));

output ->

sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]::20140320030003
0
 
LVL 12

Expert Comment

by:KRUNAL TAILOR
ID: 39941397
Hi him21,

Did you checked your physical m/c server time zone?

Thanks & Regards,
Krunal T. Tailor
0
 

Author Comment

by:Rocking
ID: 39941404
It's (UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi
0
 
LVL 27

Expert Comment

by:rrz
ID: 39941453
What does
System.out.println("user.timezone " + System.getProperty("user.timezone"));

Open in new window

print?
0
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 39941492
Try using
    Calendar calendar = Calendar.getInstance();
DateFormat df= new SimpleDateFormat("yyyyMMddHHmmss");
df.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
 String date  = df.format(calendar.getTime());
 System.out.println("date " + date);

Open in new window

0
 

Author Closing Comment

by:Rocking
ID: 39942768
I also came across the same solution at below link

http://www.coderanch.com/t/512235/java/java/Calendar-getTime
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
varialbe initialization 11 33
Opening PDF on button click and fill new document 2 38
use lov values 2 50
how to see all occupied ports on windows 10 laptop 15 65
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question