Java LocalDateTime Calculate shiftDateTime(String timex, LocalDateTime reference, boolean future)

Here you can find the source of shiftDateTime(String timex, LocalDateTime reference, boolean future)

Description

shift Date Time

License

Open Source License

Declaration

public static LocalDateTime shiftDateTime(String timex, LocalDateTime reference, boolean future) 

Method Source Code


//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;
    }
}

Related

  1. printDateTime(LocalDateTime dateTime)
  2. relativeDateTime(final LocalDateTime contextDate, final String str, final boolean add)
  3. safeCreateFromValue(LocalDateTime datetime, int year, int month, int day, int hour, int minute, int second)
  4. secondsBetween(LocalDateTime date1, LocalDateTime date2)
  5. setTime(LocalDateTime source, String time)
  6. shouldSend(Optional emailSentOpt, int timeToWait)
  7. sleepUntil(LocalDateTime next)
  8. toCalendar(final LocalDateTime localDateTime)
  9. toOffsetDateTime(Date localDateTime, TimeZone clientTimeZone)