Create a calendar object for the current time plus the time specified by the input string. - Java java.util

Java examples for java.util:Time

Description

Create a calendar object for the current time plus the time specified by the input string.

Demo Code

/*/*w  w  w .  ja v a  2  s. c o m*/
 * This document is a part of the source code and related artifacts for StilesLib, an open source library that
 * provides a set of commonly-used functions for Bukkit plugins.
 *
 * http://github.com/mstiles92/StilesLib
 *
 * Copyright (c) 2014 Matthew Stiles (mstiles92)
 *
 * Licensed under the Common Development and Distribution License Version 1.0
 * You may not use this file except in compliance with this License.
 *
 * You may obtain a copy of the CDDL-1.0 License at http://opensource.org/licenses/CDDL-1.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.
 */
//package com.java2s;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] argv) throws Exception {
        String input = "java2s.com";
        System.out.println(parseTimeDifference(input));
    }

    private static int[] calendarConstants = new int[] { Calendar.YEAR,
            Calendar.MONTH, Calendar.WEEK_OF_YEAR, Calendar.DAY_OF_YEAR,
            Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND };

    /**
     * Create a calendar object for the current time plus the time specified by the input string.
     * Example of input string format: 1h30m22s = 1 hour, 30 minutes, and 22 seconds.
     *
     * @param input the string value to parse
     * @return a calendar object for now plus the input time
     */
    public static Calendar parseTimeDifference(String input) {
        Pattern pattern = Pattern
                .compile("(?:([0-9]+)\\s*y[a-z]*[,\\s]*)?"
                        + "(?:([0-9]+)\\s*mo[a-z]*[,\\s]*)?"
                        + "(?:([0-9]+)\\s*w[a-z]*[,\\s]*)?"
                        + "(?:([0-9]+)\\s*d[a-z]*[,\\s]*)?"
                        + "(?:([0-9]+)\\s*h[a-z]*[,\\s]*)?"
                        + "(?:([0-9]+)\\s*m[a-z]*[,\\s]*)?"
                        + "(?:([0-9]+)\\s*(?:s[a-z]*)?)?",
                        Pattern.CASE_INSENSITIVE);

        int[] units = new int[] { 0, 0, 0, 0, 0, 0, 0 };
        boolean match = false;
        Matcher matcher = pattern.matcher(input);

        while (matcher.find()) {
            if (matcher.group() == null || matcher.group().isEmpty()) {
                continue;
            }

            for (int i = 0; i < matcher.groupCount(); i++) {
                if (matcher.group(i) != null && !matcher.group(i).isEmpty()) {
                    match = true;
                    break;
                }
            }

            if (match) {
                for (int i = 0; i < units.length; i++) {
                    String data = matcher.group(i + 1);
                    if (data != null && !data.isEmpty()) {
                        units[i] = Integer.parseInt(data);
                    }
                }

                break;
            }
        }

        if (!match) {
            return null;
        }

        Calendar calendar = new GregorianCalendar();
        for (int i = 0; i < units.length; i++) {
            calendar.add(calendarConstants[i], units[i]);
        }

        return calendar;
    }
}

Related Tutorials