• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Displaying the Date - Issue

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic