Here you can find the source of shiftDateTime(String timex, LocalDateTime reference, boolean future)
public static LocalDateTime shiftDateTime(String timex, LocalDateTime reference, boolean future)
//package com.java2s; //License from project: Open Source License import java.time.LocalDateTime; import java.util.Map; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; public class Main { public static LocalDateTime shiftDateTime(String timex, LocalDateTime reference, boolean future) { ImmutableMap<String, Double> timexUnitMap = resolveDurationTimex(timex); return getShiftResult(timexUnitMap, reference, future); }// w ww .ja v a2s.c om private static ImmutableMap<String, Double> resolveDurationTimex(String timex) { Builder<String, Double> resultBuilder = ImmutableMap.builder(); // resolve duration timex, such as P21DT2H(21 days 2 hours) String durationStr = timex.replace('P', '\0'); int numberStart = 0; boolean isTime = false; for (int i = 0; i < durationStr.length(); i++) { if (Character.isLetter(durationStr.charAt(i))) { if (durationStr.charAt(i) == 'T') { isTime = true; } else { String numStr = durationStr.substring(numberStart, i); try { Double number = Double.parseDouble(numStr); String srcTimexUnit = durationStr.substring(i, i + 1); if (!isTime && srcTimexUnit.equals("M")) { srcTimexUnit = "MON"; } resultBuilder.put(srcTimexUnit, number); } catch (NumberFormatException e) { return resultBuilder.build(); } } numberStart = i + 1; } } return resultBuilder.build(); } public static LocalDateTime getShiftResult(ImmutableMap<String, Double> timexUnitMap, LocalDateTime reference, boolean future) { LocalDateTime result = reference; int futureOrPast = future ? 1 : -1; for (Map.Entry<String, Double> pair : timexUnitMap.entrySet()) { String unit = pair.getKey(); Double number = pair.getValue(); switch (unit) { case "H": result = result.plusHours(Math.round(number * futureOrPast)); break; case "M": result = result.plusMinutes(Math.round(number * futureOrPast)); break; case "S": result = result.plusSeconds(Math.round(number * futureOrPast)); break; case "D": result = result.plusDays(Math.round(number * futureOrPast)); break; case "W": result = result.plusWeeks(Math.round(number * futureOrPast)); break; case "MON": result = result.plusMonths(Math.round(number * futureOrPast)); break; case "Y": result = result.plusYears(Math.round(number * futureOrPast)); break; default: return result; } } return result; } }