Java Time Format getTimeFromString(String timeStr, String format)

Here you can find the source of getTimeFromString(String timeStr, String format)

Description

get Time From String

License

Open Source License

Declaration

public static Date getTimeFromString(String timeStr, String format) 

Method Source Code


//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public final static String FORMAT_DATE_TIME = "yyyy-MM-dd hh:mm";
    private static SimpleDateFormat sdf = new SimpleDateFormat();

    public static Date getTimeFromString(String timeStr, String format) {
        if (format == null || format.trim().equals("")) {
            sdf.applyPattern(FORMAT_DATE_TIME);
        } else {/*from   ww  w.j  a  v  a  2s .c  o  m*/
            sdf.applyPattern(format);
        }
        try {
            return sdf.parse(timeStr);
        } catch (ParseException e) {
            return new Date();
        }
    }
}

Related

  1. getTimeFormat(Locale locale)
  2. getTimeFormat(String format)
  3. getTimeFormatted()
  4. getTimeFormatter()
  5. getTimeFromString(String time, SimpleDateFormat formatter)
  6. getTimeInProvenanceFormat(Date date)
  7. getTimeOnlyDateFormat()
  8. getTimeOnlyWithNumber()
  9. getTimeQuantum(String strDate, String format)