简体   繁体   English

Java将日期转换为可读形式?

[英]Java convert date to readable form?

I have Date objects stored in database. 我有Date对象存储在数据库中。 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. 我只需要DD / MM / YYYY格式。 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? 如果可能的话,我该怎么做才能获得DD / MM / YYYY而没有时间? Thanks 谢谢

只是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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