您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java的Period類如何使用”,在日常操作中,相信很多人在Java的Period類如何使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java的Period類如何使用”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
說明
Duration類通過秒和納秒相結合來描述一個時間量,最高精度是納秒。時間量可以為正也可以為負,比如1天(86400秒0納秒)、-1天(-86400秒0納秒)、1年(31556952秒0納秒)、1毫秒(0秒1000000納秒)等。
Period類通過年、月、日相結合來描述一個時間量,最高精度是天。時間量可以為正也可以為負,例如2年(2年0個月0天)、3個月(0年3個月0天)、4天(0年0月4天)等。
這兩個類是不可變的、線程安全的、最終類。都是JDK8新增的。
Duration用法
如果僅一個值表示,如使用ofDays()方法,那么其他值為0。
若僅用ofWeeks,則其天數為week數乘以7.
Period fromUnits = Period.of(3, 10, 10); Period fromDays = Period.ofDays(50); Period fromMonths = Period.ofMonths(5); Period fromYears = Period.ofYears(10); Period fromWeeks = Period.ofWeeks(40); //280天
LocalDate startDate = LocalDate.of(2015, 2, 20); LocalDate endDate = LocalDate.of(2017, 1, 15); // startDate減endDate Period period = Period.between(startDate, endDate);
格式1:“PnYnMnWnD”
P:開始符,表示period(即:表示年月日);
Y:year;
M:month;
W:week;
D:day
P, Y, M, W, D都可以用大寫或者小寫。
Period period = Period.parse("P2Y"); //2年 Period period = Period.parse("P2Y3M5D"); //2年3月5天 Period period = Period.parse("P1Y2M3W4D"); // 1年2月3周4天。即:1年2月25天
源碼
public final class Period implements ChronoPeriod, Serializable { //----------------------------------------------------------------------- /** * Obtains a {@code Period} from a text string such as {@code PnYnMnD}. * <p> * This will parse the string produced by {@code toString()} which is * based on the ISO-8601 period formats {@code PnYnMnD} and {@code PnW}. * <p> * The string starts with an optional sign, denoted by the ASCII negative * or positive symbol. If negative, the whole period is negated. * The ASCII letter "P" is next in upper or lower case. * There are then four sections, each consisting of a number and a suffix. * At least one of the four sections must be present. * The sections have suffixes in ASCII of "Y", "M", "W" and "D" for * years, months, weeks and days, accepted in upper or lower case. * The suffixes must occur in order. * The number part of each section must consist of ASCII digits. * The number may be prefixed by the ASCII negative or positive symbol. * The number must parse to an {@code int}. * <p> * The leading plus/minus sign, and negative values for other units are * not part of the ISO-8601 standard. In addition, ISO-8601 does not * permit mixing between the {@code PnYnMnD} and {@code PnW} formats. * Any week-based input is multiplied by 7 and treated as a number of days. * <p> * For example, the following are valid inputs: * <pre> * "P2Y" -- Period.ofYears(2) * "P3M" -- Period.ofMonths(3) * "P4W" -- Period.ofWeeks(4) * "P5D" -- Period.ofDays(5) * "P1Y2M3D" -- Period.of(1, 2, 3) * "P1Y2M3W4D" -- Period.of(1, 2, 25) * "P-1Y2M" -- Period.of(-1, 2, 0) * "-P1Y2M" -- Period.of(-1, -2, 0) * </pre> * * @param text the text to parse, not null * @return the parsed period, not null * @throws DateTimeParseException if the text cannot be parsed to a period */ public static Period parse(CharSequence text) { // 其他代碼 } // 其他代碼 }
獲得年月日
period.getYears(); period.getMonths(); period.getDays();
用between來比較日期。
LocalDate startDate = LocalDate.of(2015, 2, 20); LocalDate endDate = LocalDate.of(2017, 1, 15); // startDate減endDate Period period = Period.between(startDate, endDate); // 任何一個時間單元為負數,則返回true。true:endDate早于startDate period.isNegative()
Period period = Period.parse("P2Y3M5D"); period.plusDays(50); period.minusMonths(2);
Period period = Period.parse("P1Y2M3D"); period.toTotalMonths(); // 14
Period period = Period.parse("P1Y2M3D"); period.getYears(); // 1 period.getMonths(); // 2 period.getDays(); // 3
到此,關于“Java的Period類如何使用”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。