Java Parse Date parseDate(String dateOfString, String targetFormat)

Here you can find the source of parseDate(String dateOfString, String targetFormat)

Description

parse Date

License

Open Source License

Declaration

public static Date parseDate(String dateOfString, String targetFormat) 

Method Source Code


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

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

import java.util.Date;

public class Main {

    public static Date parseDate(String dateOfString, String targetFormat) {
        if (dateOfString == null || dateOfString.trim().equals("")) {
            return null;
        }//from   w ww  .  ja  va 2  s.  co m
        if (dateOfString != null && dateOfString.length() > 19) {
            dateOfString = dateOfString.substring(0, 19);
        }
        //      String format = DateUtil.getPattern(targetFormat);
        if (targetFormat == null) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(targetFormat);
        Date date = null;
        try {
            date = simpleDateFormat.parse(dateOfString);
            simpleDateFormat = new SimpleDateFormat(targetFormat);
            date = simpleDateFormat.parse(simpleDateFormat.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}

Related

  1. parseDate(String date, String pattern)
  2. parseDate(String date, String pattern, TimeZone zone)
  3. parseDate(String dateAsString)
  4. parseDate(String dateAsString)
  5. parseDate(String dateFormat, String value)
  6. parseDate(String dateStr)
  7. parseDate(String datestr)
  8. parseDate(String dateStr)
  9. parseDate(String dateStr)