Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java Calendar printing wrong Time Zone?

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Java Calendar printing wrong Time Zone?

    Hey guys,

    I am trying to create a simple java program that prints out the date that was submitted. I started playing around and noticed that the Calendar class prints exactly what I want. However. my program has to be printed showing GMT time.

    My output has to be in the following format:


    The date you entered is: Thu Mar 25 00:00:00 GMT 2003
    However, no matter how much I set the time zone to GMT, the program still outputs this:

    Thu Aug 29 03:04:01 BOT 2013
    I dont understand why it prints BOT, when even the debugger shows me that the variable has a time zone of GMT. Here is my code:


    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.util.TimeZone;
     
     
    public class DatePrinter 
    {
    	public static void main(String[] args)
    	{
    		TimeZone time = TimeZone.getTimeZone("GMT");
    		//Date cal = Calendar.getInstance(time).getTime();
     
    		Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    		System.out.println(cal.getTime());
    	}
    }

    What am I doing wrong?

    Thank you.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Java Calendar printing wrong Time Zone?

    You're printing out cal.getTime(), which returns a Date. Date Objects don't have a timezone, so by default it gives you whatever timezone you're in.

    Check out the SimpleDateFormat class for better options.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    20
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Java Calendar printing wrong Time Zone?

    What input has been given?

    would you share codes on the input part as i would like to try learning this one too.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,410
    My Mood
    Mellow
    Thanks
    85
    Thanked 473 Times in 464 Posts

    Default Re: Java Calendar printing wrong Time Zone?

    @malayo: the code posted is sufficient, no other inputs are required. You should study that code, read the Calendar and Date APIs, and follow a tutorial on Calendar/Date/time to better understand.

Similar Threads

  1. Calendar help - who is wrong, me or Netbeans?
    By wren10514 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 19th, 2012, 09:59 AM
  2. Calendar.MONTH return wrong value
    By bczm8703 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 5th, 2011, 09:33 AM
  3. getTimeInMillis() from the Calendar class returns wrong values
    By 16mydream in forum Java Theory & Questions
    Replies: 2
    Last Post: February 16th, 2011, 12:29 PM
  4. printing output to console & to a text file at the same time...
    By prasanna in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 26th, 2009, 03:43 AM