• Post Reply Bookmark Topic Watch Topic
  • New Topic

Displaying the Date - Issue  RSS feed

 
Viswas majrekar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eveyone,

Can you please anyone give me solution for this below issue.

I am trying to print the System current date.

In java code it is printing correct date, while displaying on the browser TextFiled it is showing the previous date.


For this i used the code like

String Date_Format = "MM-dd-yyyy";

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(Date_Format);

Date date=sdf.parse(sdf.format(cal.getTime()));

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Viswas majrekar wrote:Date date=sdf.parse(sdf.format(cal.getTime()));

Why are you formatting and then immediately parsing the date again? If you need a Date object, just do:

If you need to convert a Date object to a String, format it:

parse() does the opposite of format() - it converts a String to a Date object. First formatting and then immediately parsing a Date object effectively does nothing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!