得到(GregorianCalendar.HOUR)返回12小时的时间。 Java的

时间:2013-12-11 05:22:01

标签: java class

我在论坛上搜索过,其他人也遇到了同样的问题,但解决方案并不适用于我。我有两个问题。我无法正确显示am_pm并且GregorianCalendar.HOUR返回24小时值而不是12。

这是我的代码:

package lab11;

import java.util.*;
public class Lab11 {


 public static void main(String[] args) {

    Calendar time = new GregorianCalendar(); //create new gregorian calendar object
    int month = time.get(GregorianCalendar.MONTH); // create variables
    int year = time.get(GregorianCalendar.YEAR);
    int DOM = time.get(GregorianCalendar.DAY_OF_MONTH);
    int DOW = time.get(GregorianCalendar.DAY_OF_WEEK);
    int hour = time.get(GregorianCalendar.HOUR);
    int minute = time.get(GregorianCalendar.MINUTE);
    int second = time.get(GregorianCalendar.SECOND);
    long setMill = 1234567898765L;
    System.out.println(hour);
    String dayofw = "";
    int am_pm = time.get(GregorianCalendar.AM_PM);
    switch (am_pm) {
        case  Calendar.AM:
            System.out.println("AM");
            break;
        default:
            System.out.println("PM");
            break;
       }
    if (hour == 0){
        hour = 12;
    }

    switch(DOW){ //switch statement to translate numeric value of DOW to string value
        case 1: dayofw = "Sunday";
            break;
        case 2: dayofw = "Monday";
            break;
        case 3: dayofw = "Tuesday";
            break;
        case 4: dayofw = "Wednesday";
            break;
        case 5: dayofw = "Thursday";
            break;
        case 6: dayofw = "Friday";
            break;
        case 7: dayofw = "Saturday";
            break;

    }
    if (minute >= 0 && minute <= 9){ //add an extra 0 infront of minutes for formatting
    System.out.println("Today's date is " +  (month + 1) + "/" + (DOM) + "/" + year);
    System.out.println("Today's day is " + dayofw + ", and the time is " + hour + ":0" + minute + (am_pm == Calendar.AM ? "am" : "pm") + " and " + second + " seconds" );
    System.out.println("Today's date in MILLISECONDS = " + time.getTimeInMillis() + "\n");
    }
    else{ //if minutes are 10 or above don't add a 0 infront of minutes
    System.out.println("Today's date is " +  (month + 1) + "/" + (DOM) + "/" + year);
    System.out.println("Today's day is " + dayofw + ", and the time is " + hour + ":" + minute + (am_pm == Calendar.AM ? "am" : "pm")+ " and " + second + " seconds" );
    System.out.println("Today's date in MILLISECONDS = " + time.getTimeInMillis() + "\n");   
    }
    GregorianCalendar setTime = new GregorianCalendar(); //create object for the set date
    setTime.setTimeInMillis(setMill); //set time in milliseconds to the set value
    month = setTime.get(GregorianCalendar.MONTH); //update values for variables
    year = setTime.get(GregorianCalendar.YEAR);
    DOM = setTime.get(GregorianCalendar.DAY_OF_MONTH);
    int DOW2 = setTime.get(GregorianCalendar.DAY_OF_WEEK);
    switch(DOW2){
        case 1: dayofw = "Sunday";
            break;
        case 2: dayofw = "Monday";
            break;
        case 3: dayofw = "Tuesday";
            break;
        case 4: dayofw = "Wednesday";
            break;
        case 5: dayofw = "Thursday";
            break;
        case 6: dayofw = "Friday";
            break;
        case 7: dayofw = "Saturday";
            break;

    }

    hour = setTime.get(GregorianCalendar.HOUR);
    minute = setTime.get(GregorianCalendar.MINUTE);
    second = setTime.get(GregorianCalendar.SECOND);
    int am_pm2 = time.get(GregorianCalendar.AM_PM);


     if (minute >= 0 && minute <= 9){ //add an extra 0 infront of minutes for formatting
    System.out.println("The set date is " + (month) + "/" + (DOM) + "/" + year);
    System.out.println("The set day is " + dayofw + ", and the time is " + hour + ":0" + minute + (am_pm2 == Calendar.AM ? "am" : "pm") + " and " + second + " seconds" );
    System.out.println("The set date in MILLISECONDS = " + setMill);
    }
    else{ //if minutes are 10 or above don't add a 0 infront of minutes
    System.out.println("The set date is " + (month) + "/" + (DOM) + "/" + year);
    System.out.println("The set day is " + dayofw + ", and the time is " + hour + ":" + minute + (am_pm2 == Calendar.AM ? "am" : "pm") + " and " + second + " seconds" );
    System.out.println("The set date in MILLISECONDS = " + setMill);  
    }

    }

}

2 个答案:

答案 0 :(得分:4)

如果您花时间阅读JavaDocs,您会发现

  

HOUR
获取和设置的字段编号,表示小时   上午或下午。

实际上你真正想要的是Calendar.HOUR_OF_DAY

  

HOUR_OF_DAY
获取和设置的字段编号,表示小时数   那天。 HOUR_OF_DAY用于24小时制。例如,在   10:04:15.250 PM HOUR_OF_DAY是22。

GregorianCalendar.AM_PM是您传递给Calendar.get的字段值,将返回Calendar.AMCalendar.PM ...

更新了示例

Calendar time = Calendar.getInstance();
int month = time.get(Calendar.MONTH); // create variables
int year = time.get(Calendar.YEAR);
int DOM = time.get(Calendar.DAY_OF_MONTH);
int DOW = time.get(Calendar.DAY_OF_WEEK);
int hour = time.get(Calendar.HOUR);
int hourOfDay = time.get(Calendar.HOUR_OF_DAY);
int minute = time.get(Calendar.MINUTE);
int second = time.get(Calendar.SECOND);
long setMill = 1234567898765L;
String ampm = "";
String dayofw = "";
int am_pm = time.get(Calendar.AM_PM);

System.out.print(month + "/" + year + ", " + hour + ":" + minute + "." + second + " ");

switch (am_pm) {
    case  Calendar.AM:
        System.out.println("AM");
        break;
    default:
        System.out.println("PM");
        break;
}

System.out.println(month + "/" + year + ", " + hourOfDay + ":" + minute + "." + second + " " + (am_pm == Calendar.AM ? "AM" : "PM"));

...输出

11/2013, 4:30.20 PM
11/2013, 16:30.20 PM

<强>更新

我会小心使用像...这样的东西。

switch(DOW){ //switch statement to translate numeric value of DOW to string value
    case 1: dayofw = "Sunday";
        break;
    case 2: dayofw = "Monday";
        break;
    case 3: dayofw = "Tuesday";
        break;
    case 4: dayofw = "Wednesday";
        break;
    case 5: dayofw = "Thursday";
        break;
    case 6: dayofw = "Friday";
        break;
    case 7: dayofw = "Saturday";
        break;
}

由于并非所有日历都在同一天开始,相反,我会使用更像......

switch(DOW){ //switch statement to translate numeric value of DOW to string value
    case Calendar.SUNDAY: dayofw = "Sunday";
        break;
    .
    .    
    .
}

答案 1 :(得分:-3)

(对不起,如果我迟到了)因为你正在寻找一种方法来获得12而不是零(午夜时间),只需添加一个用于检测小时的开关语句(它非常麻烦,但是应该工作)

public int getTwelveHourFormat(int hourToGet) {
     switch(hourToGet) {
           case 0: return 12;
               break;
           case 13: return 1;
               break;
           case 14: return 2;
               break;
           case 15: return 3;
               break;
           case 16: return 4;
               break;
           case 17: return 5;
               break;
           case 18: return 6;
               break;
           case 19: return 7;
               break;
           case 20: return 8;
               break;
           case 21: return 9;
               break;
           case 22: return 10;
               break;
           case 23: return 11;
               break;
           default: return hourToGet;
               break;
     }
}

现在你所要做的就是打电话

int hour = getTwelveHourFormat(time.get(Calendar.HOUR_OF_DAY));

你应该好好去。