java 如何在java中将日期格式2017-02-08 00:00:00.0转换为08/02/2017?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42089796/
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-11-03 06:21:41  来源:igfitidea点击:

How to convert date format 2017-02-08 00:00:00.0 to 08/02/2017 in java?

javadate

提问by Shiva Goud A

I want to convert Date format from 2017-02-08 00:00:00.0 to dd/MM/yyyy(08/02/2017). I tried with the following code.

我想将日期格式从 2017-02-08 00:00:00.0 转换为 dd/MM/yyyy(08/02/2017)。我尝试使用以下代码。

          String dateInString  =bean.getDate();
          Date date = null;
          SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

          if (bean.getDate().matches("^[0-9]{2,4}(-[0-9]{1,2}){2}\s[0-9]{1,2}(:[0-9]{1,2}){2}\.[0-9]{1,}$")) {
                try {
                    date = formatter.parse(dateInString);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
          } 

But I am getting NullPointerException in date = formatter.parse(dateInString);line.

但是我得到了 NullPointerException date = formatter.parse(dateInString);

回答by Poornima

You can use the below code:

您可以使用以下代码:

SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat format2 = new SimpleDateFormat("dd/MM/yyyy");
String date = "2017-02-08 00:00:00.0";
try {
    Date dateNew = format1.parse(date);
    String formatedDate = format2.format(dateNew);
    System.out.println(formatedDate);
} catch (ParseException e) {
    e.printStackTrace();
}

回答by Basil Bourque

The other Answers are outdated, using troublesome old date-time classes such as SimpleDateFormat.these old classs are now legacy, supplanted by the java.time classes.

其他答案已经过时,使用麻烦的旧日期时间类,例如SimpleDateFormat.these old classes 现在是遗留的,由 java.time 类取代。

LocalDateTime

LocalDateTime

Parse your input string as a LocalDateTimeas the input lacks any indication of offset-from-UTC or time zone.

将您的输入字符串解析为 a,LocalDateTime因为输入缺少任何从 UTC 或时区偏移的指示。

The input string nearly complies with standard ISO 8601 formatting. The standard formats are used by default with the java.time classes. Replace the SPACE in middle with a T.

输入字符串几乎符合标准 ISO 8601 格式。默认情况下,标准格式与 java.time 类一起使用。将中间的 SPACE 替换为T.

String input = "2017-02-08 00:00:00.0".replace( " " , "T" );
LocalDateTime ltd = LocalDateTime.parse( input );

LocalDate

LocalDate

Extract a date-only object.

提取仅限日期的对象。

LocalDate ld = ltd.toLocalDate();

DateTimeFormatter

DateTimeFormatter

Generate a string representing that object's value. Specify your desired formatting pattern.

生成表示该对象值的字符串。指定所需的格式模式。

Locale l = Locale.US ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ).withLocale( l );
String output = ld.format( f );

回答by Shekhar Jadhav

create a SimpleDateFormat object and use it to parse Strings to Date and to format Dates to Strings. If you've tried SimpleDateFormat and it didn't work, then please show your code and any errors you may receive.

创建一个 SimpleDateFormat 对象并使用它来将字符串解析为日期并将日期格式化为字符串。如果您尝试过 SimpleDateFormat 但它不起作用,那么请显示您的代码以及您可能收到的任何错误。

MM and mm both are different. MM points Month, mm points minutes
Some example for months

MM和mm都是不同的。MM 分月,mm 分分钟 月份的
一些例子

'M' - 7  (without prefix 0 if it is single digit)
'M' - 12
'MM' - 07 (with prefix 0 if it is single digit)
'MM' - 12
'MMM' - Jul (display with 3 character)
'MMMM' - December (display with full name)

Some example for minutes

分钟的一些例子

'm' - 3  (without prefix 0 if it is single digit)
'm' - 19
'mm' - 03 (with prefix 0 if it is single digit)
'mm' - 19

回答by naro

String dateInString = "2017-02-08 00:00:00.0";
Date date = null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
    date = formatter.parse(dateInString);
} catch (ParseException e) {
      e.printStackTrace();
}
SimpleDateFormat formatter2 = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(formatter2.format(date));