Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get day, month and year separately using SimpleDateFormat

I have a SimleDateFormat like this

SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy  hh:mm");
String date = format.format(Date.parse(payback.creationDate.date));

I'm giving date with the format like "Jan,23,2014".

Now, I want to get day, month and year separately. How can I implement this?

like image 672
John Error Avatar asked Apr 10 '14 13:04

John Error


2 Answers

If you need to get the values separately, then use more than one SimpleDateFormat.

SimpleDateFormat dayFormat = new SimpleDateFormat("dd");
String day = dayFormat.format(Date.parse(payback.creationDate.date));

SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
String month = monthFormat .format(Date.parse(payback.creationDate.date));

etc.

like image 96
Duncan Jones Avatar answered Sep 19 '22 19:09

Duncan Jones


Use this to parse "Jan,23,2014"

SimpleDateFormat fmt = new SimpleDateFormat("MMM','dd','yyyy"); 
Date dt = fmt.parse("Jan,23,2014");

then you can get whatever part of the date.

like image 20
John Ding Avatar answered Sep 19 '22 19:09

John Ding