今天在做一个时间的时候,发现老是少12个小时。检查了好一会儿,才发现是Calendar.HOUR_OF_DAY与Calendar.HOUR导致的不同。
String newStr = "1332391500000";
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(Long.parseLong(newStr));
System.out.println(cal.getTime());
System.out.println(cal.get(Calendar.YEAR) + "-"
+ format(cal.get(Calendar.MONTH) + 1, 2) + "-"
+ format(cal.get(Calendar.DATE), 2) + " "
+ format(cal.get(Calendar.HOUR_OF_DAY), 2) + ":"
+ format(cal.get(Calendar.MINUTE), 2) + ":"
+ format(cal.get(Calendar.SECOND), 2));
文档已经解释的很清楚了。
Calendar.HOUR_OF_DAY是:
Field number for get
and set
indicating the hour of
the day. HOUR_OF_DAY
is used for the 24-hour clock. E.g., at
10:04:15.250 PM the HOUR_OF_DAY
is 22.
Calendar.HOUR是:
Field number for get
and set
indicating the hour of
the morning or afternoon. HOUR
is used for the 12-hour clock (0 -
11). Noon and midnight are represented by 0, not by 12. E.g., at 10:04:15.250 PM
the HOUR
is 10.
分享到:
相关推荐
System.out.println("HOUR_OF_DAY:" + calendar.get(Calendar.HOUR_OF_DAY)); System.out.println("MINUTE:" + calendar.get(Calendar.MINUTE)); System.out.println("SECOND:" + calendar.get(Calendar.SECOND)); ...
int hour = calendar.get(Calendar.HOUR_OF_DAY); // 使用HOUR_OF_DAY获取24小时制的小时 int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); ``` 请注意,`month`字段的值...
public void onCreate(Bundle savedInstanceState) { super.onCreate... mHour = c.get(Calendar.HOUR_OF_DAY); mMinute = c.get(Calendar.MINUTE); setDateTime(); setTimeOfDay(); }
cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return getNowTime("yyyy-MM-dd"); } ``` 此方法通过设置具体的月份来...
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); int milliSecond = calendar.get(Calendar.MILLISECOND); ``` 2....
cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); startTime = timeToTimestamp(getDateString(cal)); // 获取本周日的日期 cal.add(Calendar.DAY_OF_MONTH, 6...
例如,`get(Calendar.YEAR)`将返回年份,`get(Calendar.MONTH)`返回月份(0-11),`get(Calendar.HOUR_OF_DAY)`返回24小时制的小时。 标签"java"表明这是一个关于Java编程的问题。在Java中,处理日期和时间通常涉及...
利用系统服务做到闹钟的功能 private Intent intent; private PendingIntent pendingIntent; ... }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), false).show(); } }
int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小时制的小时 int minute = calendar.get(Calendar.MINUTE); // 分钟 int second = calendar.get(Calendar.SECOND); // 秒 ``` 这里我们使用`get()`方法并传入...
cd.set(Calendar.HOUR_OF_DAY,18); cd.set(Calendar.MINUTE,5); cd.set(Calendar.SECOND,00); Date date = cd.getTime(); System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date)); ...
int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); Log.e("time_now", ""+year+"_"+month+1+"_"+day+"_"+hour+"_"+minute); SimpleDateFormat formatter = new SimpleDateFormat...
- **获取小时(24小时制)**:使用`get(Calendar.HOUR_OF_DAY)`方法可以获得24小时制下的小时数。 ```java System.out.println("24小时制下的小时:" + c.get(Calendar.HOUR_OF_DAY)); ``` - **获取小时(12小时...
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); calendar.set(Calendar.MINUTE, minute); // 更新UI或者其他逻辑 } }; ``` 创建TimePickerDialog并显示: ```java TimePickerDialog timePickerDialog = ...
int hour = c.get(Calendar.HOUR_OF_DAY); int min = c.get(Calendar.MINUTE); int sec = c.get(Calendar.SECOND); ``` - **设置Calendar实例**:通过`c.setTime(date)`方法将之前创建的`Date`对象赋值给`Calendar`...
- `set()`:设置指定字段的值,如 `cal.set(Calendar.HOUR_OF_DAY, 12)`。 - `before()` 和 `after()`:比较两个日期的先后关系。 - `getTimeInMillis()`:获取 `Date` 对象表示的时间点的毫秒值。 - `clone()`...
calendar.set(Calendar.HOUR_OF_DAY, 0); // 设置小时 calendar.set(Calendar.MINUTE, 0); // 设置分钟 calendar.set(Calendar.SECOND, 0); // 设置秒 calendar.set(Calendar.MILLISECOND, 0); // 设置毫秒 long ...
this.cal.set(Calendar.HOUR_OF_DAY, 0); this.cal.set(Calendar.MINUTE, 0); this.cal.set(Calendar.SECOND, 0); this.cal.set(Calendar.MILLISECOND, 0); this.fiducialDate = this.cal.getTime(); } ...
Calendar.getInstance().get(Calendar.HOUR_OF_DAY), // 当前小时 Calendar.getInstance().get(Calendar.MINUTE), // 当前分钟 false // 是否24小时制 ); timePickerDialog.show(); // 显示对话框 ``` ### 3. ...
int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); ``` ##### 3. 判断当前月份的最大天数 可以使用`Calendar`类的`...
int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 now.set(Calendar.DATE, first_day_of_week); return now.getTime(); } /** * ...