Java Calendar Minute getMinutes(String expression, Calendar time)

Here you can find the source of getMinutes(String expression, Calendar time)

Description

get Minutes

License

Apache License

Declaration

public static Date getMinutes(String expression, Calendar time) 

Method Source Code

//package com.java2s;
/**// ww  w .  ja  va  2 s.co  m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Calendar;
import java.util.Date;

public class Main {
    public static Date getMinutes(String expression, Calendar time) {
        int hr;
        int mins;
        int day;
        int month;
        Calendar cal = Calendar.getInstance();
        cal.setTime(time.getTime());
        if (expression.contains("now")) {
            hr = getInt(expression, 0);
            mins = getInt(expression, 1);
            cal.add(Calendar.HOUR, hr);
            cal.add(Calendar.MINUTE, mins);
        } else if (expression.contains("today")) {
            hr = getInt(expression, 0);
            mins = getInt(expression, 1);
            cal.add(Calendar.HOUR, hr - (cal.get(Calendar.HOUR_OF_DAY)));
            cal.add(Calendar.MINUTE, mins);
        } else if (expression.contains("yesterday")) {
            hr = getInt(expression, 0);
            mins = getInt(expression, 1);
            cal.add(Calendar.HOUR, hr - (cal.get(Calendar.HOUR_OF_DAY)) - 24);
            cal.add(Calendar.MINUTE, mins);
        } else if (expression.contains("currentMonth")) {
            day = getInt(expression, 0);
            hr = getInt(expression, 1);
            mins = getInt(expression, 2);
            cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1, 0, 0);
            cal.add(Calendar.HOUR, 24 * day + hr);
            cal.add(Calendar.MINUTE, mins);
        } else if (expression.contains("lastMonth")) {
            day = getInt(expression, 0);
            hr = getInt(expression, 1);
            mins = getInt(expression, 2);
            cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) - 1, 1, 0, 0);
            cal.add(Calendar.HOUR, 24 * day + hr);
            cal.add(Calendar.MINUTE, mins);
        } else if (expression.contains("currentYear")) {
            month = getInt(expression, 0);
            day = getInt(expression, 1);
            hr = getInt(expression, 2);
            mins = getInt(expression, 3);
            cal.set(cal.get(Calendar.YEAR), 1, 1, 0, 0);
            cal.add(Calendar.MONTH, month - 1);
            cal.add(Calendar.HOUR, 24 * day + hr);
            cal.add(Calendar.MINUTE, mins);
        } else if (expression.contains("lastYear")) {
            month = getInt(expression, 0);
            day = getInt(expression, 1);
            hr = getInt(expression, 2);
            mins = getInt(expression, 3);
            cal.set(cal.get(Calendar.YEAR) - 1, 1, 1, 0, 0);
            cal.add(Calendar.MONTH, month - 1);
            cal.add(Calendar.HOUR, 24 * day + hr);
            cal.add(Calendar.MINUTE, mins);
        }
        return cal.getTime();
    }

    private static int getInt(String expression, int position) {
        String numbers = expression.substring(expression.indexOf('(') + 1, expression.indexOf(')'));
        return Integer.parseInt(numbers.split(",")[position]);
    }
}

Related

  1. clearHoursMinutesSecondsMillis(Calendar calendar)
  2. getFirstMinuteOfDay(Calendar dt)
  3. getMinute(Calendar cal)
  4. getMinutes(Calendar cal)
  5. getMinutes(Calendar cal1, Calendar cal2)
  6. getMinutesInterval(Calendar start, Calendar end)
  7. getOffsetInMinutes(Calendar cal)
  8. isNow(Calendar cal, Locale locale, boolean minuteCheck)
  9. minuteFloor(Calendar calendar)