Java Date Parse dateTime(String s)

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

Description

date Time

License

Apache License

Declaration

public static Date dateTime(String s) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import static java.util.Arrays.asList;

public class Main {
    public static final TimeZone UTC = TimeZone.getTimeZone("UTC");

    public static Date dateTime(String s) {
        List<DateFormat> formats = asList(dateTimeFormat("kk:mm dd/MM/yyyy", UTC),
                dateTimeFormat("kk:mm:ss dd/MM/yyyy", UTC), dateTimeFormat("kk:mm:ss.SSS dd/MM/yyyy", UTC),
                dateTimeFormat("E MMM dd kk:mm:ss Z yyyy", UTC));
        for (DateFormat format : formats) {
            try {
                return format.parse(s);
            } catch (ParseException ignored) {
            }//from  ww  w  .j  av a  2s .co m
        }
        throw new RuntimeException("Failed to parse string as dateTime: " + s);
    }

    public static DateFormat dateTimeFormat(String pattern, TimeZone timeZone) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        format.setTimeZone(timeZone);
        return format;
    }
}

Related

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