Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date format conversion as a Date object in a particular format in java

Tags:

java

date

I am finding the current time using Date date3 = Calendar.getInstance().getTime(); This gives me Thu Oct 25 11:42:22 IST 2012

Now I want my Date to be in the format 2012.10.25 and that too as a Date object and not a string.

I tried using the below code

DateFormat df = new SimpleDateFormat("yyyy.MM.dd");
Date startDate = df.parse(c_date1);

But when I finally use System.out.println(startDate.toString()); it again gives me Thu Oct 25 00:00:00 IST 2012. that is practically because the toString() function has been implemented in a way to show this format.

So is there any other way to get the date as 2012.10.25 and that too as the Date format. Date object is required because it is to be saved in db as a date field.

like image 583
saurabh j Avatar asked Dec 08 '25 17:12

saurabh j


1 Answers

you need to use df.format(Date) method to get date in required format

like image 160
NIKUNJ SANGHADIA Avatar answered Dec 10 '25 08:12

NIKUNJ SANGHADIA