當前位置: 首頁>>代碼示例>>Java>>正文


Java LocalDateTime.truncatedTo方法代碼示例

本文整理匯總了Java中java.time.LocalDateTime.truncatedTo方法的典型用法代碼示例。如果您正苦於以下問題:Java LocalDateTime.truncatedTo方法的具體用法?Java LocalDateTime.truncatedTo怎麽用?Java LocalDateTime.truncatedTo使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.time.LocalDateTime的用法示例。


在下文中一共展示了LocalDateTime.truncatedTo方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: truncate

import java.time.LocalDateTime; //導入方法依賴的package包/類
public static LocalDateTime truncate(LocalDateTime time, ChronoUnit unit,
                                     int stepRate, DayOfWeek firstDayOfWeek) {
    switch (unit) {
        case DAYS:
            return adjustField(time, DAY_OF_YEAR, stepRate).truncatedTo(unit);
        case HALF_DAYS:
            return time.truncatedTo(unit);
        case HOURS:
            return adjustField(time, HOUR_OF_DAY, stepRate).truncatedTo(unit);
        case MINUTES:
            return adjustField(time, MINUTE_OF_HOUR, stepRate)
                    .truncatedTo(unit);
        case SECONDS:
            return adjustField(time, SECOND_OF_MINUTE, stepRate).truncatedTo(
                    unit);
        case MILLIS:
            return adjustField(time, MILLI_OF_SECOND, stepRate).truncatedTo(
                    unit);
        case MICROS:
            return adjustField(time, MICRO_OF_SECOND, stepRate).truncatedTo(
                    unit);
        case NANOS:
            return adjustField(time, NANO_OF_SECOND, stepRate)
                    .truncatedTo(unit);
        case MONTHS:
            return time
                    .with(MONTH_OF_YEAR,
                            Math.max(
                                    1,
                                    time.get(MONTH_OF_YEAR)
                                            - time.get(MONTH_OF_YEAR)
                                            % stepRate)).withDayOfMonth(1)
                    .truncatedTo(DAYS);
        case YEARS:
            return adjustField(time, ChronoField.YEAR, stepRate).withDayOfYear(
                    1).truncatedTo(DAYS);
        case WEEKS:
            return time.with(DAY_OF_WEEK, firstDayOfWeek.getValue()).truncatedTo(
                    DAYS);
        case DECADES:
            int decade = time.getYear() / 10 * 10;
            return time.with(ChronoField.YEAR, decade).withDayOfYear(1)
                    .truncatedTo(DAYS);
        case CENTURIES:
            int century = time.getYear() / 100 * 100;
            return time.with(ChronoField.YEAR, century).withDayOfYear(1)
                    .truncatedTo(DAYS);
        case MILLENNIA:
            int millenium = time.getYear() / 1000 * 1000;
            return time.with(ChronoField.YEAR, millenium).withDayOfYear(1)
                    .truncatedTo(DAYS);
        default:
    }

    return time;
}
 
開發者ID:dlemmermann,項目名稱:CalendarFX,代碼行數:57,代碼來源:Util.java


注:本文中的java.time.LocalDateTime.truncatedTo方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。