Java String to Date toDate(String date)

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

Description

A method that could convert Date object to String default format -> (yyyy-MM-dd)

License

Open Source License

Parameter

Parameter Description
date the date string

Exception

Parameter Description
ParseException an exception

Return

date object

Declaration

public static Date toDate(String date) throws ParseException 

Method Source Code

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

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

import java.util.Date;

public class Main {
    /**//  w  ww  .  j a v  a 2 s.  c  o m
     * A method that could convert Date object to String
     * default format -> (yyyy-MM-dd)
     * @param date the date string
     * @return date object
     * @throws ParseException
     */
    public static Date toDate(String date) throws ParseException {
        return toDate(date, false);
    }

    /**
     * A method that could convert Date object to String
     * @param date the date string
     * @param accurate if this flag is true, the date string should be
     *                 accurate to minute(yyyy-MM-dd hh:mm)
     *                 or else it should be accurate to day(yyyy-MM-dd)
     * @return date object
     * @throws ParseException
     */
    public static Date toDate(String date, Boolean accurate) throws ParseException {
        DateFormat df;
        if (accurate)
            df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        else
            df = new SimpleDateFormat("yyyy-MM-dd");
        return df.parse(date);
    }
}

Related

  1. toDate(String createdAt)
  2. toDate(String d)
  3. toDate(String date)
  4. toDate(String date)
  5. toDate(String date)
  6. toDate(String date)
  7. toDate(String date)
  8. toDate(String date)
  9. toDate(String date, String dateFormat)