Java Calendar類的時(shí)間操作
Java Calendar 類時(shí)間操作,這也許是創(chuàng)建日歷和管理最簡(jiǎn)單的一個(gè)方案,示范代碼很簡(jiǎn)單,演示了獲取時(shí)間,日期時(shí)間的累加和累減,以及比較。
注意事項(xiàng):
Calendar 的 month 從 0 開(kāi)始,也就是全年 12 個(gè)月由 0 ~ 11 進(jìn)行表示。
而 Calendar.DAY_OF_WEEK 定義和值如下:
Calendar.SUNDAY = 1
Calendar.MONDAY = 2
Calendar.TUESDAY = 3
Calendar.WEDNESDAY = 4
Calendar.THURSDAY = 5
Calendar.FRIDAY = 6
Calendar.SATURDAY = 7
SimpleDateFormat 的格式定義

Java Calendar 演示代碼如下:
package demo;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
public class Test
{
public Test()
{
}
public static void main(String[] args)
{
// 字符串轉(zhuǎn)換日期格式
// DateFormat fmtDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 接收傳入?yún)?shù)
// String strDate = args[1];
// 得到日期格式對(duì)象
// Date date = fmtDateTime.parse(strDate);
// 完整顯示今天日期時(shí)間
String str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(new Date());
System.out.println(str);
// 創(chuàng)建 Calendar 對(duì)象
Calendar calendar = Calendar.getInstance();
try
{
// 對(duì) calendar 設(shè)置時(shí)間的方法
// 設(shè)置傳入的時(shí)間格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d H:m:s");
// 指定一個(gè)日期
Date date = dateFormat.parse("2013-6-1 13:24:16");
// 對(duì) calendar 設(shè)置為 date 所定的日期
calendar.setTime(date);
// 按特定格式顯示剛設(shè)置的時(shí)間
str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());
System.out.println(str);
}
catch (ParseException e)
{
e.printStackTrace();
}
// 或者另一種設(shè)置 calendar 方式
// 分別爲(wèi) year, month, date, hourOfDay, minute, second
calendar = Calendar.getInstance();
calendar.set(2013, 1, 2, 17, 35, 44);
str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());
System.out.println(str);
// Calendar 取得當(dāng)前時(shí)間的方法
// 初始化 (重置) Calendar 對(duì)象
calendar = Calendar.getInstance();
// 或者用 Date 來(lái)初始化 Calendar 對(duì)象
calendar.setTime(new Date());
// setTime 類似上面一行
// Date date = new Date();
// calendar.setTime(date);
str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());
System.out.println(str);
// 顯示年份
int year = calendar.get(Calendar.YEAR);
System.out.println("year is = " + String.valueOf(year));
// 顯示月份 (從0開(kāi)始, 實(shí)際顯示要加一)
int month = calendar.get(Calendar.MONTH);
System.out.println("nth is = " + (month + 1));
// 本周幾
int week = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println("week is = " + week);
// 今年的第 N 天
int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR);
System.out.println("DAY_OF_YEAR is = " + DAY_OF_YEAR);
// 本月第 N 天
int DAY_OF_MONTH = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("DAY_OF_MONTH = " + String.valueOf(DAY_OF_MONTH));
// 3小時(shí)以后
calendar.add(Calendar.HOUR_OF_DAY, 3);
int HOUR_OF_DAY = calendar.get(Calendar.HOUR_OF_DAY);
System.out.println("HOUR_OF_DAY + 3 = " + HOUR_OF_DAY);
// 當(dāng)前分鐘數(shù)
int MINUTE = calendar.get(Calendar.MINUTE);
System.out.println("MINUTE = " + MINUTE);
// 15 分鐘以后
calendar.add(Calendar.MINUTE, 15);
MINUTE = calendar.get(Calendar.MINUTE);
System.out.println("MINUTE + 15 = " + MINUTE);
// 30分鐘前
calendar.add(Calendar.MINUTE, -30);
MINUTE = calendar.get(Calendar.MINUTE);
System.out.println("MINUTE - 30 = " + MINUTE);
// 格式化顯示
str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
System.out.println(str);
// 重置 Calendar 顯示當(dāng)前時(shí)間
calendar.setTime(new Date());
str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
System.out.println(str);
// 創(chuàng)建一個(gè) Calendar 用于比較時(shí)間
Calendar calendarNew = Calendar.getInstance();
// 設(shè)定為 5 小時(shí)以前,后者大,顯示 -1
calendarNew.add(Calendar.HOUR, -5);
System.out.println("時(shí)間比較:" + calendarNew.compareTo(calendar));
// 設(shè)定7小時(shí)以后,前者大,顯示 1
calendarNew.add(Calendar.HOUR, +7);
System.out.println("時(shí)間比較:" + calendarNew.compareTo(calendar));
// 退回 2 小時(shí),時(shí)間相同,顯示 0
calendarNew.add(Calendar.HOUR, -2);
System.out.println("時(shí)間比較:" + calendarNew.compareTo(calendar));
}
}
要計(jì)算時(shí)間差,可用 Calendar.getTimeInMillis() 取得兩個(gè)時(shí)間的微秒級(jí)的時(shí)間差,再加以換算即可,比如獲得相差天數(shù),代碼如下:
// 得微秒級(jí)時(shí)間差 long val = calendarEnd.getTimeInMillis() - calendarBegin.getTimeInMillis(); // 換算后得到天數(shù) long day = val / (1000 * 60 * 60 * 24);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java嵌套for循環(huán)的幾種常見(jiàn)優(yōu)化方案
這篇文章主要給大家介紹了關(guān)于Java嵌套for循環(huán)的幾種常見(jiàn)優(yōu)化,在Java中優(yōu)化嵌套for循環(huán)可以通過(guò)以下幾種方式來(lái)提高性能和效率,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
SpringBoot Event實(shí)現(xiàn)異步消費(fèi)機(jī)制的示例代碼
這篇文章主要介紹了SpringBoot Event實(shí)現(xiàn)異步消費(fèi)機(jī)制,ApplicationEvent以及Listener是Spring為我們提供的一個(gè)事件監(jiān)聽(tīng)、訂閱的實(shí)現(xiàn),內(nèi)部實(shí)現(xiàn)原理是觀察者設(shè)計(jì)模式,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-04-04
MybatisPlus的LambdaQueryWrapper用法詳解
LambdaQueryWrapper<Tag>?是 MyBatis-Plus 框架中的一個(gè)功能強(qiáng)大的查詢構(gòu)造器,它用于構(gòu)建 SQL 查詢條件,具有一定的參考價(jià)值,感興趣的可以了解一下2024-10-10
Springboot MDC+logback實(shí)現(xiàn)日志追蹤的方法
MDC(Mapped Diagnostic Contexts)映射診斷上下文,該特征是logback提供的一種方便在多線程條件下的記錄日志的功能,這篇文章主要介紹了Springboot MDC+logback實(shí)現(xiàn)日志追蹤的方法,需要的朋友可以參考下2024-04-04
mybatis攔截器實(shí)現(xiàn)通用權(quán)限字段添加的方法
這篇文章主要給大家介紹了關(guān)于mybatis攔截器實(shí)現(xiàn)通用權(quán)限字段添加的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用mybatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Spring Boot 3.x 全新的熱部署配置方式詳解(IntelliJ ID
這篇文章主要介紹了Spring Boot 3.x 全新的熱部署配置方式(IntelliJ IDEA 2023.1),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
java應(yīng)用開(kāi)發(fā)之JVM運(yùn)行時(shí)內(nèi)存分析
這篇文章主要介紹了java應(yīng)用開(kāi)發(fā)之JVM運(yùn)行時(shí)內(nèi)存,文中附含圖文示例內(nèi)容分析非常簡(jiǎn)要,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09

