Solved

Get System time

Posted on 2014-03-19
12
299 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
wordappend challenge 8 84
Python Assistance 7 31
eclipse formatting 6 40
JList custom Cell Renderer refresh 15 39
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now