Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get System time

Posted on 2014-03-19
12
Medium Priority
?
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 28

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 28

Expert Comment

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

Open in new window

print?
0
 
LVL 28

Accepted Solution

by:
rrz earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

721 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