Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get System time

Posted on 2014-03-19
12
Medium Priority
?
313 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

971 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