Java Date Parse datetime(String s)

Here you can find the source of datetime(String s)

Description

datetime

License

Apache License

Declaration

public static Date datetime(String s) 

Method Source Code


//package com.java2s;
/*//w w w. ja  v a2s. c om
 * Copyright 2010 Spolecne s.r.o. (www.spoledge.com)
 *
 * Licensed 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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

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

    public static Date datetime(String s) {
        return parsedate(s, FMT_DATETIME);
    }

    public static Date datetime(int year, int month, int day, int hour, int minute, int second) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, day);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, minute);
        cal.set(Calendar.SECOND, second);

        return cal.getTime();
    }

    private static Date parsedate(String s, SimpleDateFormat fmt) {
        // SimpleDateFormat is not thread safe:
        synchronized (fmt) {
            try {
                return fmt.parse(s);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

Related

  1. dateParser(String dateStr, String inputDateFormat, String outputDateFormat)
  2. dateParser(String param)
  3. dateParseShortString(Date date)
  4. dateStringToDate(String dateString, String DateStringType)
  5. dateStringToDateObject(String dateString, String dateFormat)
  6. dateTime(String s)
  7. datetimeParse(String datetime)
  8. DateTimeParse(String DateTimeString, String LMS)
  9. getDateFromCurrentAnd24HRTime(Date current, String hr24Time)