java 如何使用 SimpleDateFormat 格式化日期

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

how to format date using SimpleDateFormat

javasimpledateformat

提问by spiderman

I cannot format a date. dateFormat.format()accepts a Dateas argument. So I created a new Date()

我无法格式化日期。 dateFormat.format()接受 aDate作为参数。所以我创建了一个新的Date()

It says the below Date() method is deprecated, and I get the below exception while running.

它说不推荐使用下面的 Date() 方法,并且在运行时出现以下异常。

exception:

例外:

Exception in thread "main" java.lang.IllegalArgumentException at
java.util.Date.parse(Date.java:598)


public class MyDate {
    public static void main(String[] args) {


        Date date = new Date("2012-02-16T00:00:00.000-0500");

        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "dd-MMM-yyyy HH:mm:ss");
        String stringDate = dateFormat.format(date);

        System.out.println(stringDate); // how do I test this conversion??

    }
}

My database has date of the format - 2012-02-16T00:00:00.000-0500 I need to convert it to string of the format : dd-MMM-yyyy HH:mm:ss

我的数据库有格式的日期 - 2012-02-16T00:00:00.000-0500 我需要将它转换为格式的字符串:dd-MMM-yyyy HH:mm:ss

I'm using Java6

我正在使用 Java6

采纳答案by Andy Brown

If you want to read in the date "2012-02-16T00:00:00.000-0500" you should probably use a SimpleDateFormatto parseit like so:

如果你想在日期改为“2012-02-16T00:00:00.000-0500”你应该使用SimpleDateFormatparse它,像这样:

DateFormat parseFormat = new SimpleDateFormat(
        "yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = parseFormat.parse("2012-02-16T00:00:00.000-0500");

Along with the rest of your code this writes:

连同您的其余代码,它写道:

16-Feb-2012 05:00:00

The parse format pattern letters are listed in the SimpleDateFormatdocumentation. The Tis escaped with apostrophes.

解析格式模式字母在SimpleDateFormat文档中列出。该T转义撇号。

This answer assumes Java 7, or you would be using the new date & time APIfrom Java 8

此答案假定 Java 7,或者您将使用Java 8 中的新日期和时间 API

回答by spiderman

Thanks to @Andy Brown. In addition to what Andy Brown has answered, I'm posting the complete snippet

感谢@Andy Brown。除了安迪布朗的回答之外,我还发布了完整的片段

Complete Solution:

完整解决方案:

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

public class SampleDate {
    public static void main(String[] args) throws ParseException {
        DateFormat parseFormat = new SimpleDateFormat(
                 "yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        Date date = parseFormat.parse("2012-03-16T00:00:00.000-0500");
        String strDate = parseFormat.format(date);
        System.out.println(strDate);

        // if you get date of type 'java.sql.Date' directly from database cursor like
         //rs.getDate("created_date"), just pass it directly to format()

        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "dd-MMM-yyyy HH:mm:ss");
        String stringDate = dateFormat.format(date);
        System.out.println(stringDate);

    }
}

/*
Output:

2012-03-16T01:00:00.000-0400
16-Mar-2012 01:00:00

*/

you can also convert java.util.Dateto java.sql.Datelike this,

你也可以转换java.util.Datejava.sql.Date这样,

String dateString = "03-11-2012";
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
    java.util.Date date = dateFormat.parse(dateString);
    java.sql.Date sqlDate = new Date(date.getTime());
// set the input param type as OracleTypes.DATE and pass the input param date as sqlDate