1

我在字符串中有一个日期,我使用这个:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
dataUltima = sdf.format(sdf);

格式化日期。但此方法返回系统的实际日期。我怎样才能只格式化日期而不更改日期。

观察。我从服务器上得到这个日期,它来找我 yyy-dd-MM 我想要它dd/MM/yyyy

4

4 回答 4

2

我不确定你的问题是什么。

尝试一下。也许有帮助。

String server_format = "2013-01-02";    //server comes format ?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {

    Date date = sdf.parse(server_format);
    System.out.println(date);
    String your_format = new SimpleDateFormat("dd/MM/yyyy").format(date);
    System.out.println(your_format);    //you want format ?

} catch (ParseException e) {
    System.out.println(e.toString()); //date format error
}
于 2013-07-16T17:47:58.920 回答
0

跟随:

try
{
  //create SimpleDateFormat object with source string date format
  SimpleDateFormat sdfSource = new SimpleDateFormat("yyy-dd-MM");

  //parse the original date string(strDate) that you are getting from server into Date object
  Date date = sdfSource.parse(strDate);

  //create SimpleDateFormat object with desired date format
  SimpleDateFormat sdfDestination = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");

  //parse the date into another format
  strDate = sdfDestination.format(date);

  System.out.println("Date is converted");
  System.out.println("Converted date is : " + strDate);

}
catch(ParseException pe)
{
  System.out.println("Parse Exception : " + pe);
}

来源:使用 SimpleDateFormat 将日期字符串从一种格式转换为另一种格式

希望这可以帮助。

于 2013-07-16T17:28:33.150 回答
0

试试这个:

String date = "2011-11-12 11:11:11"; //Fill with sample date received from server
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss");
Date testDate = null;
try {
    testDate = sdf.parse(date);
}catch(Exception ex){
    ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm");
String dataUltima = formatter.format(testDate);
System.out.println("Date is "+dataUltima );
于 2013-07-16T17:29:12.023 回答
0

试试这个代码:

    Date newDate = new Date();

    // Print the date with the server format
    SimpleDateFormat sdfServer = new SimpleDateFormat("yyy-dd-MM ");
    System.out.println("Server date " + sdfServer.format(newDate));

    // Create a new formatter to get the date in the format you want
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    String dataUltima = sdf.format(newDate);
    System.out.println("System date " + dataUltima);
于 2013-07-16T17:33:46.067 回答