我有一个表示日期的字符串。

String date_s = "2011-01-18 00:00:00.0";

我想把它转换成一个日期,并以YYYY-MM-DD格式输出。

2011-01-18

我怎样才能做到这一点呢?


好吧,根据我在下面找到的答案,以下是我尝试过的一些方法:

String date_s = " 2011-01-18 00:00:00.0"; 
SimpleDateFormat dt = new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss"); 
Date date = dt.parse(date_s); 
SimpleDateFormat dt1 = new SimpleDateFormat("yyyyy-mm-dd");
System.out.println(dt1.format(date));

但是它输出02011-00-1而不是所需的2011-01-18。我做错了什么?


当前回答

从格式中删除一个y:

SimpleDateFormat dt1 = new SimpleDateFormat("yyyyy-mm-dd");

它应该是:

SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-mm-dd");

其他回答

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

答案当然是创建一个SimpleDateFormat对象,并使用它来解析“字符串到日期”并将“日期到字符串”格式化。如果您已经尝试了SimpleDateFormat,它没有工作,那么请显示您的代码和您可能收到的任何错误。

附录:String格式中的“mm”与“mm”不一样。用MM表示月,用MM表示分钟。另外,yyyyy和yyyy也不一样。例如,:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FormateDate {

    public static void main(String[] args) throws ParseException {
        String date_s = "2011-01-18 00:00:00.0";

        // *** note that it's "yyyy-MM-dd hh:mm:ss" not "yyyy-mm-dd hh:mm:ss"  
        SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = dt.parse(date_s);

        // *** same for the format String below
        SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(dt1.format(date));
    }

}

java.time

2014年3月,现代日期时间API* API取代了容易出错的java。SimpleDateFormat. util日期时间API和它们的格式化API。从那时起,强烈建议停止使用遗留API。

此外,下面引用的是Joda-Time主页的通知:

注意,从Java SE 8开始,用户被要求迁移到Java。time (JSR-310)——JDK的核心部分,取代了这个项目。

格式化不需要DateTimeFormatter

您只需要DateTimeFormatter来解析字符串,但不需要DateTimeFormatter来获得所需格式的日期。现代Date-Time API基于ISO 8601,因此是java的toString实现。时间类型返回ISO 8601格式的字符串。您想要的格式是localdate# toString的默认格式。

使用java解决方案。time,现代Date-Time API:

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strDate = "2011-01-18 00:00:00.0";
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("u-M-d H:m:s.S", Locale.ENGLISH);
        LocalDateTime ldt = LocalDateTime.parse(strDate, dtfInput);
        // Alternatively,
        // LocalDateTime ldt = dtfInput.parse(strDate, LocalDateTime::from);

        LocalDate date = ldt.toLocalDate();
        System.out.println(date);
    }
}

输出:

2011-01-18

在线演示

关于解决方案的一些重要注意事项:

java。time使得在Date-Time类型本身上调用解析和格式化函数成为可能,除了传统的方式(即在formatter类型上调用解析和格式化函数,在java中是DateTimeFormatter。时间API)。 这里,你可以用y代替u但我更喜欢u而不是y。

从Trail: Date Time了解更多关于现代Date-Time API的信息。


*无论出于何种原因,如果你必须坚持使用Java 6或Java 7,你可以使用ThreeTen-Backport,它可以向后移植大部分Java。Java 6和7的时间功能。如果你正在为一个Android项目工作,你的Android API级别仍然不兼容Java-8,检查Java 8+ API通过desugaring和如何使用ThreeTenABP在Android项目。

你可以用:

Date yourDate = new Date();

SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
String date = DATE_FORMAT.format(yourDate);

它工作得很完美!

private SimpleDateFormat dataFormat = new SimpleDateFormat("dd/MM/yyyy");

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    if(value instanceof Date) {
        value = dataFormat.format(value);
    }
    return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
};