Here you can find the source of stringToDate(String date)
Parameter | Description |
---|---|
date | to convert |
public static Date stringToDate(String date) throws ParseException
//package com.java2s; //License from project: Open Source License import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { /**//from w ww .j a v a 2 s . co m * Formats a date given as a string in a stringobject * allowed dates are * <p/> * "yyyy/MM/dd HH:mm" * "yyyy-MM-dd HH:mm" * "yyyy/MM/dd" * "yyyy-MM-dd" * * @param date to convert * @return date object */ public static Date stringToDate(String date) throws ParseException { String pattern; if (date.contains("/") && date.contains(":")) { pattern = "yyyy/MM/dd HH:mm"; } else if (date.contains("/") && !date.contains(":")) { pattern = "yyyy/MM/dd"; } else if (date.contains("-") && date.contains(":")) { pattern = "yyyy-MM-dd HH:mm"; } else if (date.contains("-") && !date.contains(":")) { pattern = "yyyy-MM-dd"; } else { pattern = "dd.mm.yyyy"; } return new SimpleDateFormat(pattern).parse(date); } }