欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JAVA獲取特定格式時(shí)間方式

 更新時(shí)間:2023年10月03日 11:39:58   作者:能一塊玩嗎丶  
我們有時(shí)要獲取時(shí)間,年月日時(shí)分秒周幾,有時(shí)要以特定的格式出現(xiàn),本文主要介紹了JAVA獲取特定格式時(shí)間方式,具有一定的參考價(jià)值,感興趣的可以了解一下

0 背景

我們有時(shí)要獲取時(shí)間,年月日時(shí)分秒周幾,有時(shí)要以特定的格式出現(xiàn)。這時(shí)就要借助 SimpleDateFormat 或者 DateTimeFormatter。有時(shí)要某個(gè)月份有多少天需要借助 Calendar。所以有必要了解一些知識(shí)。

1 SimpleDateFormat

simpledateFormat 線程不安全,DateTimeFormatter 線程安全。

// 用法
String string = new SimpleDateFormat(String PATTERN, Locale locale).format(Date date);

PATTERN 的樣式有很多。具體可以看源碼。

public static final String STAND_TIME = "yyyy-MM-dd HH:mm:ss";
public static final String FULL_TIME = "yyyy-MM-dd HH:mm:ss.SSS";
public static final String YEAR_MONTH_DAY = "yyyy-MM-dd";
public static final String YEAR_MONTH_DAY_CN = "yyyy年MM月dd日";
public static final String HOUR_MINUTE_SECOND = "HH:mm:ss";
public static final String HOUR_MINUTE_SECOND_CN = "HH時(shí)mm分ss秒";
public static final String YEAR = "yyyy";
public static final String MONTH = "MM";
public static final String DAY = "dd";
public static final String WEEK = "E";
public static final String HOUR = "HH";
public static final String MINUTE = "mm";
public static final String SECOND = "ss";
public static final String MILLISECOND = "SSS";

在這里插入圖片描述

2 Calendar

Calendar calendar = Calendar.getInstance();
// 設(shè)置一個(gè)日歷
calendar.set(Calendar.DATE,int);
calendar.set(Calendar.MONTH,int);
calendar.set(Calendar.YEAR,int);
// 年月日的增加,可正可負(fù)
calendar.add(Calendar.DATE,int);
calendar.add(Calendar.MONTH,int);
calendar.add(Calendar.YEAR,int);
// 年月日的回滾,不會(huì)影響大字段。如增加日,不會(huì)影響月,31 -> 1,不改變?cè)路荨?
calendar.roll(Calendar.DATE,int);
calendar.roll(Calendar.MONTH,int);
calendar.roll(Calendar.YEAR,int);

另外注意的參數(shù)

// week 是從星期天開始算的 1 - > 7
int a = calandar.get(Calendar.DAY_OF_WEEK);
// month 是從 0 開始的, 0 -> 11
int month = calendar.get(Calendar.MONTH);

3 時(shí)間工具 DateUtil

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Objects;
public class DateUtil {
    public static final String STAND_TIME = "yyyy-MM-dd HH:mm:ss";
    public static final String FULL_TIME = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String YEAR_MONTH_DAY = "yyyy-MM-dd";
    public static final String YEAR_MONTH_DAY_CN = "yyyy年MM月dd日";
    public static final String HOUR_MINUTE_SECOND = "HH:mm:ss";
    public static final String HOUR_MINUTE_SECOND_CN = "HH時(shí)mm分ss秒";
    public static final String YEAR = "yyyy";
    public static final String MONTH = "MM";
    public static final String DAY = "dd";
    public static final String WEEK = "E";
    public static final String HOUR = "HH";
    public static final String MINUTE = "mm";
    public static final String SECOND = "ss";
    public static final String MILLISECOND = "SSS";
    public static final String YESTERDAY = "昨天";
    public static final String TODAY = "今天";
    public static final String TOMORROW = "明天";
    /**
     * 獲得當(dāng)前時(shí)間
     *
     * @return 例如 2023-09-29 10:00:00
     */
    public static String getCurrentDateTime() {
        return new SimpleDateFormat(STAND_TIME, Locale.CHINESE).format(new Date());
    }
    /**
     * 獲得當(dāng)前完整時(shí)間
     *
     * @return 例如 2023-09-29 10:00:00.123
     */
    public static String getCurrentFullDateTime() {
        return new SimpleDateFormat(FULL_TIME, Locale.CHINESE).format(new Date());
    }
    /**
     * 獲得今天年月日
     *
     * @return 例如 2023-09-29
     */
    public static String getCurrentYearMonthDay() {
        return new SimpleDateFormat(YEAR_MONTH_DAY, Locale.CHINESE).format(new Date());
    }
    /**
     * 獲得年月日 中文版
     *
     * @return 例如 2023年9月29日
     */
    public static String getCurrentYearMonthDayCn() {
        return new SimpleDateFormat(YEAR_MONTH_DAY_CN, Locale.CHINESE).format(new Date());
    }
    /**
     * 獲得年月日 自定義分隔符
     *
     * @param delimiter 分隔符
     * @return 例如 2023/9/29
     */
    public static String getCurrentYearMonthDayDelimiter(CharSequence delimiter) {
        return new SimpleDateFormat(YEAR + delimiter + MONTH + delimiter + DAY, Locale.CHINESE).format(new Date());
    }
    /**
     * 獲得時(shí)分秒
     *
     * @return 例如 10:00:00
     */
    public static String getCurrentHourMinuteSecond() {
        return new SimpleDateFormat(HOUR_MINUTE_SECOND, Locale.CHINESE).format(new Date());
    }
    /**
     * 獲得時(shí)分秒 中文版
     *
     * @return 例如 10時(shí)00分00秒
     */
    public static String getCurrentHourMinuteSecondCn() {
        return new SimpleDateFormat(HOUR_MINUTE_SECOND_CN, Locale.CHINESE).format(new Date());
    }
    /**
     * 獲取時(shí)分秒 分隔符
     *
     * @param delimiter 分隔符
     * @return 例如 2021/07/01
     */
    public static String getCurrentHourMinuteSecondDelimiter(CharSequence delimiter) {
        return new SimpleDateFormat(HOUR + delimiter + MINUTE + delimiter + SECOND, Locale.CHINESE).format(new Date());
    }
    /**
     * 獲得年
     *
     * @return 例如 2023
     */
    public static String getCurrentYear() {
        return new SimpleDateFormat(YEAR, Locale.CHINESE).format(new Date());
    }
    /**
     * 獲得月
     *
     * @return 例如 09
     */
    public static String getCurrentMonth() {
        return new SimpleDateFormat(MONTH, Locale.CHINESE).format(new Date());
    }
    /**
     * 獲得日
     *
     * @return 29
     */
    public static String getCurrentDay() {
        return new SimpleDateFormat(DAY, Locale.CHINESE).format(new Date());
    }
    /**
     * 獲得時(shí)
     *
     * @return 例如 10
     */
    public static String getCurrentHour() {
        return new SimpleDateFormat(HOUR, Locale.CHINESE).format(new Date());
    }
    /**
     * 獲得時(shí)分秒
     *
     * @return 例如 00
     */
    public static String getCurrentMinute() {
        return new SimpleDateFormat(MINUTE, Locale.CHINESE).format(new Date());
    }
    /**
     * 獲得秒
     *
     * @return 例如 00
     */
    public static String getCurrentSecond() {
        return new SimpleDateFormat(SECOND, Locale.CHINESE).format(new Date());
    }
    /**
     * 獲得毫秒
     *
     * @return 例如 123
     */
    public static String getCurrentMillisecond() {
        return new SimpleDateFormat(MILLISECOND, Locale.CHINESE).format(new Date());
    }
    /**
     * 獲得當(dāng)前時(shí)間戳
     *
     * @return 例如 2023-9-29 10:00:00   為1695952800
     */
    public static long getCurrentTimestamp() {
        return System.currentTimeMillis();
    }
    /**
     * 將時(shí)間轉(zhuǎn)換成時(shí)間戳
     *
     * @param time 時(shí)間
     * @return 返回時(shí)間戳 long
     */
    public static long dateToStamp(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(STAND_TIME, Locale.CHINESE);
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Objects.requireNonNull(date).getTime();
    }
    /**
     * 將時(shí)間戳轉(zhuǎn)換成時(shí)間
     *
     * @param stamp 時(shí)間戳
     * @return 例如 2023-9-29 10:00:00
     */
    public static String stampToDate(long stamp) {
        return new SimpleDateFormat(STAND_TIME, Locale.CHINESE).format(stamp);
    }
    /**
     * 返回今天是星期幾
     *
     * @return 例如 周五
     */
    public static String getCurrentWeek() {
        return new SimpleDateFormat(WEEK, Locale.CHINESE).format(new Date());
    }
    /**
     * @param dateTime 日期 例如 2023-09-29
     * @return 例如 周五
     */
    public static String getWeekOf(String dateTime) {
        Date date;
        if ("".equals(dateTime)) {
            date = new Date();
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY, Locale.CHINESE);
            try {
                date = sdf.parse(dateTime);
            } catch (Exception e) {
                date = new Date();
                e.printStackTrace();
            }
        }
        return new SimpleDateFormat(WEEK,Locale.CHINESE).format(date);
    }
    /**
     * @param dateTime 日期 例如 2023-09-29
     * @return 例如 2023-09-28
     */
    public static String getYesterdayOf(String dateTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY, Locale.CHINESE);
        Date date;
        try {
            date = sdf.parse(dateTime);
        } catch (ParseException e) {
            date = null;
            e.printStackTrace();
        }
        Calendar calendar = new GregorianCalendar();
        if (date != null) {
            calendar.setTime(date);
        }
        calendar.add(Calendar.DATE, -1);
        date = calendar.getTime();
        return new SimpleDateFormat(YEAR_MONTH_DAY, Locale.CHINESE).format(date);
    }
    /**
     * 獲取輸入日期的明天
     *
     * @param dateTime 例如 2023-09-29
     * @return 例如 2023-09-30
     */
    public static String getTomorrowOf(String dateTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY, Locale.CHINESE);
        Date date;
        try {
            date = sdf.parse(dateTime);
        } catch (ParseException e) {
            date = null;
            e.printStackTrace();
        }
        Calendar calendar = new GregorianCalendar();
        if (date != null) {
            calendar.setTime(date);
        }
        calendar.add(Calendar.DATE, +1);
        date = calendar.getTime();
        return new SimpleDateFormat(YEAR_MONTH_DAY, Locale.CHINESE).format(date);
    }
    /**
     * @param dateTime 一個(gè)時(shí)間 例如 2023-9-29
     * @return 相對(duì)今天而言是什么,比如今天是2023-9-30,返回昨天。
     */
    public static String getDayInfoOf(String dateTime) {
        String dayInfo;
        String toDay = getCurrentYearMonthDay();
        String yesterday = getYesterdayOf(toDay);
        String tomorrow = getTomorrowOf(toDay);
        if (dateTime.equals(yesterday)) {
            dayInfo = YESTERDAY;
        } else if (dateTime.equals(toDay)) {
            dayInfo = TODAY;
        } else if (dateTime.equals(tomorrow)) {
            dayInfo = TOMORROW;
        } else {
            dayInfo = getWeekOf(dateTime);
        }
        return dayInfo;
    }
    /**
     * 返回當(dāng)前月份的天數(shù)
     *
     * @return 例如 9月份 返回30
     */
    public static int getCurrentDaysOfMonth() {
        Calendar calendar = new GregorianCalendar();
        //把日期設(shè)置為當(dāng)月第一天
        calendar.set(Calendar.DATE, 1);
        //日期回滾一天,也就是最后一天,roll 方法不更改大字段。
        calendar.roll(Calendar.DATE, -1);
        return calendar.get(Calendar.DATE);
    }
    /**
     * 返回指定年份月份的天數(shù)
     *
     * @param year  年份 例如 2023
     * @param month 月份 例如 09
     * @return 例如 30
     */
    public static int getDaysOfMothOf(int year, int month) {
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        //把日期設(shè)置為當(dāng)月第一天
        calendar.set(Calendar.DATE, 1);
        //日期回滾一天,也就是最后一天,roll 方法不更改大字段。
        calendar.roll(Calendar.DATE, -1);
        return calendar.get(Calendar.DATE);
    }
}

到此這篇關(guān)于JAVA獲取特定格式時(shí)間方式的文章就介紹到這了,更多相關(guān)JAVA 特定格式時(shí)間內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • IO密集型任務(wù)設(shè)置線程池線程數(shù)實(shí)現(xiàn)方式

    IO密集型任務(wù)設(shè)置線程池線程數(shù)實(shí)現(xiàn)方式

    這篇文章主要介紹了IO密集型任務(wù)設(shè)置線程池線程數(shù)實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • spring?boot?mybatis日志輸出到控制臺(tái)的方法實(shí)踐

    spring?boot?mybatis日志輸出到控制臺(tái)的方法實(shí)踐

    在開發(fā)過(guò)程中我們往往需要打印出SQL語(yǔ)句,這樣就方便我們監(jiān)控問(wèn)題,本文主要介紹了spring?boot?mybatis日志輸出到控制臺(tái)的方法實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-05-05
  • Java利用jenkins做項(xiàng)目的自動(dòng)化部署

    Java利用jenkins做項(xiàng)目的自動(dòng)化部署

    這篇文章主要介紹了Java利用jenkins做項(xiàng)目的自動(dòng)化部署,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • Kotlin 泛型詳解及簡(jiǎn)單實(shí)例

    Kotlin 泛型詳解及簡(jiǎn)單實(shí)例

    這篇文章主要介紹了 Kotlin 泛型詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • 利用Spring?boot+LogBack+MDC實(shí)現(xiàn)鏈路追蹤

    利用Spring?boot+LogBack+MDC實(shí)現(xiàn)鏈路追蹤

    這篇文章主要介紹了利用Spring?boot+LogBack+MDC實(shí)現(xiàn)鏈路追蹤,MDC?可以看成是一個(gè)與當(dāng)前線程綁定的哈希表,可以往其中添加鍵值對(duì),下文詳細(xì)介紹需要的小伙伴可以參考一下
    2022-04-04
  • 實(shí)現(xiàn)分布式WebSocket集群的方法

    實(shí)現(xiàn)分布式WebSocket集群的方法

    本文總結(jié)出了幾個(gè)實(shí)現(xiàn)分布式WebSocket集群的辦法,從zuul到spring cloud gateway的不同嘗試,總結(jié)出了這篇文章,希望能幫助到某些人,并且能一起分享這方面的想法與研究
    2022-03-03
  • Mybatis如何解決sql中l(wèi)ike通配符模糊匹配問(wèn)題

    Mybatis如何解決sql中l(wèi)ike通配符模糊匹配問(wèn)題

    這篇文章主要介紹了Mybatis如何解決sql中l(wèi)ike通配符模糊匹配問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • springboot配置Jackson返回統(tǒng)一默認(rèn)值的實(shí)現(xiàn)示例

    springboot配置Jackson返回統(tǒng)一默認(rèn)值的實(shí)現(xiàn)示例

    在項(xiàng)目開發(fā)中,我們返回的數(shù)據(jù)或者對(duì)象沒(méi)有的時(shí)候一般直接返回的null,那么如何返回統(tǒng)一默認(rèn)值,感興趣的可以了解一下
    2021-07-07
  • Spring?Cloud?通過(guò)?Gateway?webflux實(shí)現(xiàn)網(wǎng)關(guān)異常處理

    Spring?Cloud?通過(guò)?Gateway?webflux實(shí)現(xiàn)網(wǎng)關(guān)異常處理

    在某一個(gè)服務(wù)中出現(xiàn)異常,通過(guò)@ControllerAdvice?+?@ExceptionHandler?統(tǒng)一異常處理,即使在微服務(wù)架構(gòu)中,也可以將上述統(tǒng)一異常處理放入到公共的微服務(wù)中,這樣哪一個(gè)微服務(wù)需要,直接引入模塊,本文重點(diǎn)介紹Spring?Cloud?通過(guò)?Gateway?webflux實(shí)現(xiàn)網(wǎng)關(guān)異常處理,一起看看吧
    2023-11-11
  • 利用Java實(shí)現(xiàn)圖片轉(zhuǎn)化為ASCII圖的示例代碼

    利用Java實(shí)現(xiàn)圖片轉(zhuǎn)化為ASCII圖的示例代碼

    本文將詳細(xì)講解如何利用 Java 實(shí)現(xiàn)圖片轉(zhuǎn)化為 ASCII 圖,從項(xiàng)目背景與意義、相關(guān)技術(shù)知識(shí),到系統(tǒng)需求與架構(gòu)設(shè)計(jì),再到詳細(xì)實(shí)現(xiàn)思路、完整代碼和代碼解讀,最后對(duì)項(xiàng)目進(jìn)行總結(jié)與展望,需要的朋友可以參考下
    2025-03-03

最新評(píng)論