Java 日期格式化程序

发布于 2024-10-30 16:11:43 字数 114 浏览 1 评论 0原文

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

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

I am getting date format as "YYYY-mm-dd hh:mm" as formatter object.

How can I format the input formatter object to get only "YYYY-mm-dd";?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

当爱已成负担 2024-11-06 16:11:43

我得到的日期格式为
“YYYY-mm-dd hh:mm”作为格式化程序
目的。我如何格式化输入
仅获取格式化程序对象
“年-月-日”;

您的日期不能为 YYYY-mm-dd,而应为 yyyy-MM-dd。要获取 yyyy-MM-dd 格式的日期,代码如下:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(todaysDate);

I am getting date format as
"YYYY-mm-dd hh:mm" as formatter
object. How can i format the input
formatter object to get only
"YYYY-mm-dd";

You can not have date as YYYY-mm-dd it should be yyyy-MM-dd. To get date in yyyy-MM-dd following is the code:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(todaysDate);
最初的梦 2024-11-06 16:11:43
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();
        }
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();
        }
心头的小情儿 2024-11-06 16:11:43

使用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)));

Use 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)));
童话里做英雄 2024-11-06 16:11:43

如果您收到格式为 "YYYY-mm-dd hh:mm" 的日期,并且希望将其设置为 "YYYY-mm-dd",我建议您只需使用 inputDate.substring(0, 10)。

无论哪种方式,都要小心潜在的Y10k bug :)

If you're getting a date in the format "YYYY-mm-dd hh:mm" and you want it as "YYYY-mm-dd" I suggest you just use inputDate.substring(0, 10).

In either way, beware of potential Y10k bugs :)

_蜘蛛 2024-11-06 16:11:43

Java 中的示例格式日期为 yyyy-MM-dd

Format formatter = new SimpleDateFormat("yyyy-MM-dd");
Calendar now = Calendar.getInstance();
System.out.println("Now: "+formatter.format(now.getTime()) );

Following sample formate date as yyyy-MM-dd in Java

Format formatter = new SimpleDateFormat("yyyy-MM-dd");
Calendar now = Calendar.getInstance();
System.out.println("Now: "+formatter.format(now.getTime()) );
三生一梦 2024-11-06 16:11:43

java.time

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

对于解析输入,定义一个格式化程序:

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

教程链接

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

java.time

I recommend that you use java.time, the modern Java date and time API, for your date and time work.

For parsing input define a formatter:

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

Parse:

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

Output so far:

2019-01-21T23:45

Format output:

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

2019-01-21

Tutorial link

Trail: Date Time (The Java™ Tutorials) explaining how to use java.time.

旧时模样 2024-11-06 16:11:43

SimpleDateFormat 是你在寻找什么。

SimpleDateFormat is what you're looking for.

神仙妹妹 2024-11-06 16:11:43

试试这个:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(todaysDate);

Try this:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(todaysDate);
夏天碎花小短裙 2024-11-06 16:11:43

使用此代码:

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");

Use this code:

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");
一身仙ぐ女味 2024-11-06 16:11:43

其他答案(例如 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 );

转储到控制台...

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

运行时...

dateTime: 2011-04-07T00:00:00.000+01:00
stringOut: 2011-04-07

Other answers such as the one by user2663609 are correct.

As an alternative, the third-part open-source replacement for the java.util.Date/Calendar classes, Joda-Time, includes a built-in format for your needs.

// © 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 );

Dump to console…

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

When run…

dateTime: 2011-04-07T00:00:00.000+01:00
stringOut: 2011-04-07
一念一轮回 2024-11-06 16:11:43

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

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;    
} 

This question has so many good answers !! , here comes another one more generic solution

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;    
} 
少女情怀诗 2024-11-06 16:11:43
 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();
    }
 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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文