Results 1 to 3 of 3
  1. #1
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default String to Calendar

    Okay, so I'm having an issue with this little bit of conversion. I'm converting a string (_dateString) into a Calendar time. I am using DateFormat and SimpleDateFormat to accomplish this task. Everything seems to be working great, except for it figuring out whether it is AM or PM. According to SimpleDateFormat (Java Platform SE 7 ) I am using "aa" to get my AM or PM marker. How come in my output then, it believes it to be 4:45 am instead of 4:45 pm? Hour of Day should return the 24 hour clock, which should show 16 instead of 4. I have posted the output below my code.

    Java Code:
    import java.text.*;
    import java.util.*;
    
    public class Time{
    	static String _dateString = "08 Feb 2014, 4:45pm";
      
    	public static void main(String args[]){
    		Calendar cal=Calendar.getInstance();
    		try {  
    			DateFormat formatter ; 
    			Date date ; 
    			formatter = new SimpleDateFormat("dd MMM yyyy, HH:mmaa");
    			date = (Date)formatter.parse(_dateString); 
    			cal.setTime(date);
    			System.out.println("Today is " + date );
    		  } catch (ParseException e){
    			  System.out.println("Exception :" + e); 
    		  } 
    
    		System.out.println("Year: " + cal.get(Calendar.YEAR));
    		System.out.println("Month: " + cal.get(Calendar.MONTH));
    		System.out.println("Day of Month: " + cal.get(Calendar.DAY_OF_MONTH));
    		System.out.println("Day of Week: " + cal.get(Calendar.DAY_OF_WEEK));
    		System.out.println("Week Of Year: " + cal.get(Calendar.WEEK_OF_YEAR));
    		System.out.println("Week of Month: " + cal.get(Calendar.WEEK_OF_MONTH));
    		System.out.println("Hour: " + cal.get(Calendar.HOUR));
    		System.out.println("Hour of Day: " + cal.get(Calendar.HOUR_OF_DAY));
    		System.out.println("Minute: " + cal.get(Calendar.MINUTE));
    		System.out.println("Second: " + cal.get(Calendar.SECOND));
    		System.out.println("Millisecond: " + cal.get(Calendar.MILLISECOND);
    		
    	}//End Main
    Output:
    Java Code:
    Today is Sat Feb 08 04:45:00 EST 2014
    Year: 2014
    Month: 1
    Day of Month: 8
    Day of Week: 7
    Week Of Year: 6
    Week of Month: 2
    Hour: 4
    Hour of Day: 4
    Minute: 45
    Second: 0
    Millisecond: 0
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,024
    Rep Power
    6

    Default Re: String to Calendar

    You're specifying HH which is hour in day (0-23). You should be specifying hh.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: String to Calendar

    Ah, so simple yet I wasn't looking anywhere near it. I thought for sure there was some issue with aa somehow. Thanks for the help, I will make sure to triple check the whole string next time.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. Calendar now = Calendar.getInstance()
    By volkvanmyn25 in forum New To Java
    Replies: 3
    Last Post: 10-31-2011, 11:25 PM
  2. Replies: 4
    Last Post: 09-21-2011, 05:39 PM
  3. Calendar Help Please
    By loopsnhoops in forum New To Java
    Replies: 4
    Last Post: 05-24-2011, 11:35 AM
  4. Convert String to Calendar
    By Lil_Aziz1 in forum New To Java
    Replies: 5
    Last Post: 08-06-2010, 04:02 PM
  5. Web calendar
    By Daniel in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 06-27-2007, 06:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •