10

我将日期格式"YYYY-mm-dd hh:mm"作为格式化程序对象。

如何将输入格式化程序对象格式化为仅获取"YYYY-mm-dd";

4

13 回答 13

25

我将日期格式设置为“YYYY-mm-dd hh:mm”作为格式化程序对象。如何格式化输入格式化程序对象以仅获取“YYYY-mm-dd”;

您不能将日期设置为YYYY-mm-dd它应该是yyyy-MM-dd。要在 yyyy-MM-dd 中获取日期,代码如下:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(todaysDate);
于 2011-04-07T12:28:48.137 回答
6
Format formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Date date;
        try {
            date = (Date)((DateFormat) formatter).parse("2011-04-13 05:00");
            formatter = new SimpleDateFormat("yyyy-MM-dd");
            String s = formatter.format(date);
            System.out.println(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
于 2011-04-07T12:29:33.833 回答
3

利用SimpleDateFormat

String myDateString = "2009-04-22 15:51";

SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");

System.out.println(outFormat.format(inFormat.parse(myDateString)));
于 2011-04-07T12:29:39.067 回答
2

如果你得到格式的日期"YYYY-mm-dd hh:mm"并且你想要它,"YYYY-mm-dd"我建议你只使用inputDate.substring(0, 10).

无论哪种方式,请注意潜在的Y10k 错误:)

于 2011-04-07T12:30:06.340 回答
1

是的,SimpleDateFormat 是您正在寻找的 http://dlc.sun.com.edgesuite.net/jdk/jdk-api-localizations/jdk-api-zh-cn/builds/latest/html/en/api/java /text/SimpleDateFormat.html

于 2015-04-23T08:31:54.887 回答
1

在 Java 中遵循示例格式日期为 yyyy-MM-dd

Format formatter = new SimpleDateFormat("yyyy-MM-dd");
Calendar now = Calendar.getInstance();
System.out.println("Now: "+formatter.format(now.getTime()) );
于 2011-04-07T12:47:45.510 回答
0

其他答案,例如user2663609的答案是正确的。

作为替代方案,java.util.Date/Calendar 类的第三方开源替代品Joda-Time包括满足您需求的内置格式。

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;

String stringIn = "2011-04-07";

// Returns a formatter for a full date as four digit year, two digit month of year, and two digit day of month (yyyy-MM-dd).
DateTimeFormatter formatter = ISODateTimeFormat.date().withZone( DateTimeZone.forID( "Europe/London" ) ).withLocale( Locale.UK );
DateTime dateTime = formatter.parseDateTime( stringIn ).withTimeAtStartOfDay();
String stringOut = formatter.print( dateTime );

转储到控制台...</p>

System.out.println( "dateTime: " + dateTime.toString() );
System.out.println( "stringOut: " + stringOut );

运行时……</p>

dateTime: 2011-04-07T00:00:00.000+01:00
stringOut: 2011-04-07
于 2013-12-20T07:02:51.103 回答
0

使用此代码:

Date date=new Date();

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

String formattedDate = formatter.format(date);

System.out.println("formatted time==>" + formattedDate);

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
于 2013-12-20T05:28:05.250 回答
0

SimpleDateFormat就是你要找的。

于 2011-04-07T12:29:34.527 回答
0

尝试这个:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(todaysDate);
于 2013-03-18T11:00:03.073 回答
0

这个问题有很多好的答案!, 这是另一个更通用的解决方案

public static String getDateInFormate(String oldFormate , String newFormate , String dateToParse){
    //old "yyyy-MM-dd hh:mm"
    //new yyyy-MM-dd
    //dateTopars 2011-04-13 05:00  
    String formatedDate="";
    Format formatter = new SimpleDateFormat();
    Date date;
    try {
        date = (Date)((DateFormat) formatter).parse(dateToParse);
        formatter = new SimpleDateFormat(newFormate);
        formatedDate = formatter.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return formatedDate;    
} 
于 2015-05-19T10:55:30.663 回答
0

java.time

我建议您使用现代 Java 日期和时间 API java.time 进行日期和时间工作。

为了解析输入定义一个格式化程序:

private static final DateTimeFormatter FORMATTER
        = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm", Locale.ROOT);

解析:

    String input = "2019-01-21 23:45";
    LocalDateTime dateTime = LocalDateTime.parse(input, FORMATTER);
    System.out.println(dateTime);

到目前为止的输出:

2019-01-21T23:45

格式输出:

    String output = dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE);
    System.out.println(output);

2019-01-21

教程链接

Trail:日期时间(Java™ 教程)解释如何使用 java.time。

于 2021-12-05T11:26:37.957 回答
0
 SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                String strDate = entry_date;
                System.out.println("strDate*************"+strDate);
                Date date = null;
                try {
                    date = sdf.parse(strDate);

                } catch (ParseException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

                Date yesterday =subtractDay( date);
                String requiredDate = df.format(yesterday);
                System.out.println("110 days before*******************"+requiredDate);

public static Date subtractDay(Date date) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, -110);`enter code here`
        return cal.getTime();
    }
于 2017-01-21T07:28:34.020 回答