Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

GregorianCalendar and 12pm

Dear Experts:

I am trying to display the time in 12-hour format using GregorianCalendar. It works fine on all times except 12pm. Instead of showing 12, it shows 0. How can this be fixed?
 
import java.util.* ;

public class CalendarTest
{
public static void main(String[] argv)
{
   int hour, minute, second;
   String time;
                  
  Calendar calendar = new GregorianCalendar () ;
   
    //for 12-hour clock
    calendar.set(Calendar.AM_PM, Calendar.AM);
   
    // Set current hour, minute and second    
   
    hour = calendar.get ( Calendar.HOUR_OF_DAY );        
    minute = calendar.get ( Calendar.MINUTE );
    second = calendar.get ( Calendar.SECOND );
   
   time = hour + ":" + minute + ":" + second;
   
   System.out.println("Current time " + time);
      
  }
}
0
njava
Asked:
njava
1 Solution
 
CEHJCommented:
Easier to use a DateFormat:

SimpleDateFormat sdf = new SimpleDateFormat("h:mm");
System.out.println(sdf.format(calendar.getTime());
0
 
nadhuviCommented:
       SimpleDateFormat sdf;
        sdf = new SimpleDateFormat("yyyy-MM-dd hh:ss:mm.SSS");      
        String effectiveDate = sdf.format(new Date());
0
 
zzynxSoftware engineerCommented:
>> It works fine on all times except 12pm
I think 12pm simply doesn't exist.

        Calendar cal = Calendar.getInstance();
        cal.set(2005, 1, 1, 23, 59, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
        System.out.println(sdf.format(cal.getTime()));
        cal.set(2005, 1, 1, 0, 0, 0);
        System.out.println(sdf.format(cal.getTime()));

This outputs:
11:59 PM
12:00 AM
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
zzynxSoftware engineerCommented:
>> I think 12pm simply doesn't exist.
Correction: 12pm is noon, 12am is midnight

01:00 = 1 AM
11:00 = 11 AM
11:59 = 11:59 AM
12:00 = 12 PM
12:30 = 12 PM
13:00 = 1 PM
23:00 = 11 PM
00:00 = 12 AM
00:01 = 12:01 AM
0
 
zzynxSoftware engineerCommented:
>> I think 12pm simply doesn't exist.
I meant at midnight
0
 
zzynxSoftware engineerCommented:
12:30 = 12:30 PM of course
0
 
CEHJCommented:
Since i see you want hour minutes seconds that sdf should be:

SimpleDateFormat sdf = new SimpleDateFormat("h:mm:ss");
0
 
zzynxSoftware engineerCommented:
... and since he's talking about am/pm:

SimpleDateFormat sdf = new SimpleDateFormat("h:mm:ss a");

;°)
0
 
njavaAuthor Commented:
If I use SimpleDateFormat, how do I get the values for hour, minute and second for 24-hour clock and 12-hour clock?
0
 
CEHJCommented:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); // 24

SimpleDateFormat sdf = new SimpleDateFormat("h:mm:ss"); // 12

0
 
CEHJCommented:
:-)
0

Featured Post

Industry Leaders: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now