Here you can find the source of getMinutes(String expression, Calendar time)
public static Date getMinutes(String expression, Calendar time)
//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]); } }