Here you can find the source of toDate(String string)
public static Date toDate(String string)
//package com.java2s; /*//ww w.ja v a 2 s. com Copyright 2010 Inexas. All rights reserved. Licensed under the Inexas Software License V1.0. You may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.inexas.com/ISL-V1.0. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import java.text.*; import java.util.*; public class Main { private final static DateFormat dateTimeFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); private final static DateFormat dateOnlyFormatter = new SimpleDateFormat("yyyy/MM/dd"); private final static DateFormat timeOnlyFormatter = new SimpleDateFormat("HH:mm:ss"); private final static String DATE_MESSAGE = // "Date should be in format 'yyyy/MM/dd HH:mm:ss', you may have " + // "date only, time only or date and time all units in descending " + // "order: "; public static Date toDate(String string) { int whichFormatter = 0; if (string.indexOf('/') > 0) { whichFormatter |= 0x01; } if (string.indexOf(':') > 0) { whichFormatter |= 0x02; } final DateFormat formatter; switch (whichFormatter) { case 1: formatter = dateOnlyFormatter; break; case 2: formatter = timeOnlyFormatter; break; case 3: formatter = dateTimeFormatter; break; default: throw new RuntimeException(DATE_MESSAGE + string); } try { final Date result = formatter.parse(string); return result; } catch (ParseException e) { throw new RuntimeException(DATE_MESSAGE + string, e); } } }