Java Parse Date parseDateTime(String d, String t)

Here you can find the source of parseDateTime(String d, String t)

Description

parse Date Time

License

Open Source License

Parameter

Parameter Description
d MMDDAA
t HHMMSS

Return

Date Object

Declaration

public static Date parseDateTime(String d, String t) 

Method Source Code

//package com.java2s;
/*//  ww  w .j a v  a 2s. c o m
 * jPOS Project [http://jpos.org]
 * Copyright (C) 2000-2012 Alejandro P. Revilla
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Date;
import java.util.Locale;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Main {
    static SimpleDateFormat dfDateTime = (SimpleDateFormat) DateFormat
            .getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM,
                    Locale.US);

    public static Date parseDateTime(String s) throws ParseException {
        if (s == null)
            return null;
        return dfDateTime.parse(s);
    }

    /**
     * @param d MMDDAA
     * @param t HHMMSS
     * @return Date Object
     */
    public static Date parseDateTime(String d, String t) {
        Calendar cal = new GregorianCalendar();
        Date now = new Date();
        cal.setTime(now);

        int YY = Integer.parseInt(d.substring(4));
        int MM = Integer.parseInt(d.substring(0, 2)) - 1;
        int DD = Integer.parseInt(d.substring(2, 4));
        int hh = Integer.parseInt(t.substring(0, 2));
        int mm = Integer.parseInt(t.substring(2, 4));
        int ss = Integer.parseInt(t.substring(4));
        int century = cal.get(Calendar.YEAR) / 100;

        cal.set(Calendar.YEAR, (century * 100) + YY);
        cal.set(Calendar.MONTH, MM);
        cal.set(Calendar.DATE, DD);
        cal.set(Calendar.HOUR_OF_DAY, hh);
        cal.set(Calendar.MINUTE, mm);
        cal.set(Calendar.SECOND, ss);

        //
        // I expect this program to continue running by 2099 ... --apr@jpos.org
        //
        Date thisCentury = cal.getTime();
        cal.set(Calendar.YEAR, (--century * 100) + YY);
        Date previousCentury = cal.getTime();

        if (Math.abs(now.getTime() - previousCentury.getTime()) < Math
                .abs(now.getTime() - thisCentury.getTime()))
            thisCentury = previousCentury;
        return thisCentury;
    }
}

Related

  1. parseDateSubset(String value, String[] parsePatterns, Locale l, TimeZone tz)
  2. parseDateTime(@Nullable String s)
  3. parseDateTime(final String dateTime)
  4. parseDateTime(String _date)
  5. parseDateTime(String currDate, String format)
  6. parseDateTime(String date)
  7. parseDateTime(String date, String format, String locale, String timeZone)
  8. parseDateTime(String date, String time)
  9. parseDateTime(String dateTime)