Java Parse Date parseDateTime(String fullDateParam, String unixDateParam, Map request)

Here you can find the source of parseDateTime(String fullDateParam, String unixDateParam, Map request)

Description

Parses datetime (firstly formated by DATE_FORMAT then unix format).

License

Open Source License

Parameter

Parameter Description
fullDateParam a parameter
unixDateParam a parameter
request a parameter

Declaration

public static Calendar parseDateTime(String fullDateParam, String unixDateParam, Map<String, String[]> request)
        throws IllegalArgumentException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

public class Main {
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");

    /**/*from w ww  .  ja v  a2 s.  co m*/
     * Parses datetime (firstly formated by DATE_FORMAT then unix format).
     * Throws exception if fails.
     * 
     * @param fullDateParam
     * @param unixDateParam
     * @param request
     * @return
     */
    public static Calendar parseDateTime(String fullDateParam, String unixDateParam, Map<String, String[]> request)
            throws IllegalArgumentException {
        String[] fullDateStrArr = request.get(fullDateParam);
        if (fullDateStrArr != null && fullDateStrArr.length == 1) {
            String dateStr = fullDateStrArr[0];
            return parseFullDate(fullDateParam, dateStr);
        }

        String[] unixDateStrArr = request.get(unixDateParam);
        if (unixDateStrArr != null && unixDateStrArr.length == 1) {
            String dateStr = unixDateStrArr[0];
            return parseUnixDate(unixDateParam, dateStr);
        }

        throw new IllegalArgumentException("Missing date parameter. Use \"" + fullDateParam + "\"(with format "
                + DATE_FORMAT.toPattern() + ") or \"" + unixDateParam + "\" param");
    }

    /**
     * Tries to parse full formated date. Throws exception if fails.
     * 
     * @param paramName
     * @param dateStr
     * @return
     */
    public static Calendar parseFullDate(String paramName, String dateStr) throws IllegalArgumentException {
        Calendar day;
        try {
            day = Calendar.getInstance();
            Date date = DATE_FORMAT.parse(dateStr);
            day.setTime(date);
        } catch (Exception e) {
            throw new IllegalArgumentException(
                    "Unparsable \"" + paramName + "\". Must be in format " + DATE_FORMAT.toPattern(), e);
        }

        return day;
    }

    /**
     * Tries to parse unix date. Throws exception if fails.
     * 
     * @param paramName
     * @param dateStr
     * @return
     */
    public static Calendar parseUnixDate(String paramName, String dateStr) throws IllegalArgumentException {
        Calendar day;
        try {
            day = Calendar.getInstance();
            long ms = Long.parseLong(dateStr);
            day.setTimeInMillis(ms);
        } catch (Exception e) {
            throw new IllegalArgumentException("Unparsable \"" + paramName + "\". Must be UTC time in ms", e);
        }

        return day;
    }
}

Related

  1. parseDateTime(String dateTimeStr)
  2. parseDateTime(String dateTimeString)
  3. parseDateTime(String dateTimeString)
  4. parseDateTime(String dateTimeString, String[] validFormats, String outputFormat)
  5. parseDateTime(String format, String dateStr)
  6. parseDateTime(String s)
  7. parseDateTime(String s)
  8. parseDateTime(String s)
  9. parseDateTime(String s)