Java examples for java.util:Date Parse
Converts a String in the format "yyyy-MM-dd" to a Calendar object.
//package com.java2s; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; public class Main { public static void main(String[] argv) throws Exception { String dateString = "java2s.com"; System.out.println(parse(dateString)); }//from ww w .j a v a 2 s.co m /** * Default date format in the form 2013-03-18. */ private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat( "yyyy-MM-dd"); /** * Converts a String in the format "yyyy-MM-dd" to a Calendar object. * * Returns null if the String could not be converted. * * @param dateString the date as String * @return the calendar object or null if it could not be converted */ public static Calendar parse(String dateString) { Calendar result = Calendar.getInstance(); try { result.setTime(DATE_FORMAT.parse(dateString)); return result; } catch (ParseException e) { return null; } } }