Java 如何将日期转换为字符串 MM/dd/yyyy 格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22294235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:47:13  来源:igfitidea点击:

How to convert date in to String MM/dd/yyyy Format?

javadatesimpledateformatdate-formatting

提问by Rajesh

I have a Date 2014-10-01 00:00:00.0 I have to convert into 10/01.2014 How can i?

我有一个日期 2014-10-01 00:00:00.0 我必须转换成 10/01.2014 我该怎么做?

采纳答案by Mohammad Nurdin

how about this one

这个怎么样

try
 {
    String currentDate = "2014-10-01 00:00:00.0";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
    Date tempDate=simpleDateFormat.parse(currentDate);
    SimpleDateFormat outputDateFormat = new SimpleDateFormat("dd/MM.YYYY");           
    System.out.println("Output date is = "+outputDateFormat.format(tempDate));
  } catch (ParseException ex) 
  {
        System.out.println("Parse Exception");
  }

回答by Ravi Godara

try This

尝试这个

// Create an instance of SimpleDateFormat used for formatting 
  // the string representation of date (month/day/year)
  DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

 // Get the date today using Calendar object.
   Date today = Calendar.getInstance().getTime();        
    // Using DateFormat format method we can create a string 
  // representation of a date with the defined format.
  String reportDate = df.format(today);

   // Print what date is today!
System.out.println("Report Date: " + reportDate);

回答by Shriram

Use SimpleDateFormat:

使用简单日期格式:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String date = sdf.format("Your date");

回答by mtbomb

It's pretty easy if you use the JodaTime library.

如果您使用 JodaTime 库,这很容易。

    DateTime dt = new DateTime();
    DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd.YYYY");
    String str = fmt.print(dt);

回答by Salah

try this:

尝试这个:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date d = sdf.parse("dd/MM.YYYY");