Java String to Date toDate(String date, String format)

Here you can find the source of toDate(String date, String format)

Description

to Date

License

LGPL

Declaration

public static Date toDate(String date, String format) 

Method Source Code

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

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

import java.util.Calendar;

import java.util.Date;

public class Main {
    public static Date toDate(String date, String format) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, 0);/*ww w  .  ja  va  2s .  co  m*/
        c.set(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR, 12);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date d;
        try {
            d = sdf.parse(date);
        } catch (ParseException e) {
            throw new IllegalArgumentException("unable to parse date from [" + date + "]", e);
        }
        c.setTimeInMillis(d.getTime());
        // log.debug("Parsed ["+c.getTime()+"] from ['"+date+"','"+format+"']");
        Date dateType = c.getTime();
        assert null != dateType;
        return dateType;
    }
}

Related

  1. toDate(String date)
  2. toDate(String date)
  3. toDate(String date, String dateFormat)
  4. toDate(String date, String format)
  5. toDate(String date, String format)
  6. toDate(String dateStr)
  7. toDate(String dateStr)
  8. toDate(String dateStr, String format)
  9. toDate(String dateStr, String pattern)