?
Solved

GregorianCalendar and 12pm

Posted on 2005-03-11
11
Medium Priority
?
306 Views
Last Modified: 2012-06-27
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
Comment
Question by:njava
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13515191
Easier to use a DateFormat:

SimpleDateFormat sdf = new SimpleDateFormat("h:mm");
System.out.println(sdf.format(calendar.getTime());
0
 
LVL 1

Expert Comment

by:nadhuvi
ID: 13515250
       SimpleDateFormat sdf;
        sdf = new SimpleDateFormat("yyyy-MM-dd hh:ss:mm.SSS");      
        String effectiveDate = sdf.format(new Date());
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13515292
>> 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
Technology Partners: 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 37

Expert Comment

by:zzynx
ID: 13515337
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 13515343
>> I think 12pm simply doesn't exist.
I meant at midnight
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13515349
12:30 = 12:30 PM of course
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13515557
Since i see you want hour minutes seconds that sdf should be:

SimpleDateFormat sdf = new SimpleDateFormat("h:mm:ss");
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13516084
... and since he's talking about am/pm:

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

;°)
0
 
LVL 1

Author Comment

by:njava
ID: 13522261
If I use SimpleDateFormat, how do I get the values for hour, minute and second for 24-hour clock and 12-hour clock?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 13523987
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); // 24

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

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13528331
:-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

771 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