简体   繁体   中英

Java convert date to readable form?

I have Date objects stored in database. Now am when the form I am displaying presents all the data in my objects, it also displays date. I don't want it to show the long detailed form. All I need is DD/MM/YYYY format. This is what am getting now:

Sun Jan 01 00:00:00 GMT+03:00 2012

Now I followed an online tutorial and got this code going:

 public String changeDateFormat(Date date){
    String pattern = "MM/dd/yyyy";
    //Date datex = null;
    SimpleDateFormat format = new SimpleDateFormat(pattern);
    try {
        date = format.parse(date.toString());
        System.out.println(date);

} catch (ParseException e) {
  System.err.print(e);
}
     return date.toString();
}

and I invoke it when am getting the value of global variable:

  public String getEndDate() {
    return changeDateFormat(endDate);
  }

the output shown above is the result. What can I do to only and only get DD/MM/YYYY without time if possible? Thanks

只是

return (new SimpleDateFormat("dd/MM/yyyy").format(dateInstance));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM