Question

How can i parse the following string 2000-05-12T12:00 to year, month, date, hourOfDay, minute, second?

I know to parse split it in "-" but there is efficient way to date for all the value?

I saw the solution like SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");

but I need to update it to java calendar and i dont know how to do that?

Was it helpful?

Solution

You would need to use SimpleDateFormat. Have a look at the java docs. Also you would have to look at the Calendar class. Java docs are here.

Try this -

String str = "2000-05-12T12:00"; 
SimpleDateFormat parserSDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
Date d = parserSDF.parse(str);

Calendar cal = Calendar.getInstance();
cal.setTime(d);

System.out.println(cal.get(Calendar.DAY_OF_MONTH));
System.out.println(cal.get(Calendar.MONTH)); // month in the Calendar class begins from 0
System.out.println(cal.get(Calendar.YEAR));
System.out.println(cal.get(Calendar.HOUR_OF_DAY));
System.out.println(cal.get(Calendar.MINUTE));
System.out.println(cal.get(Calendar.SECOND));

OTHER TIPS

try use from public SimpleDateFormat(String pattern) function.

read this page and (http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html#parse(java.lang.String, java.text.ParsePosition) for do it!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top