Solved

Get System time

Posted on 2014-03-19
12
305 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 116
String array comparison 4 38
JAVA API design with micro service cloud in mind 1 46
Notify sent to other threads in Java 9 33
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

860 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