Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Displaying the Date - Issue  RSS feed

 
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()));

 
Java Cowboy
Sheriff
Posts: 16084
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!