Java Joda 时间 - 月份的日期和月份不返回 2 位数输出

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

Joda Time - Day of month and Month of Year Not returning 2 digit output

javajodatime

提问by Shantanoo K

I have following code:

我有以下代码:

String dateUTC = "2013-09-08T10:23:54.663-04:00";
org.joda.time.DateTime dateTime = new DateTime(dateUTC);
System.out.println(" Year : " + dateTime.getYear());
System.out.println(" Month : " + dateTime.getMonthOfYear());
System.out.println(" Day : " + dateTime.getDayOfMonth()); 

The Output of this program is :
Year : 2013
Month : 9 // I want this to be 2 digit if the month is between 1 to 9
Day : 8 // I want this to be 2 digit if the month is between 1 to 9

Is there any way I can retrieve the value of month and year in 2 digit using Joda API.

有什么方法可以使用 Joda API 以 2 位数字检索月份和年份的值。

采纳答案by upog

An alternative way would be using decimal formater

另一种方法是使用十进制格式器

 DecimalFormat df = new DecimalFormat("00");

==========================================================================================

================================================== ========================================

import java.text.DecimalFormat;
import org.joda.time.DateTime;
public class Collectionss {
    public static void main(String[] args){
        DecimalFormat df = new DecimalFormat("00");
        org.joda.time.DateTime dateTime = new DateTime();
        System.out.println(" Year : "+dateTime.getYear());      
        System.out.println(" Month : "+ df.format(dateTime.getMonthOfYear()));
        System.out.println(" Day : "+dateTime.getDayOfMonth()); 
    }

}

回答by Jon Skeet

You're calling getMonthOfYear()- that just returns an int. What value could it possibly return for a month earlier than Octoberwhich would satisfy you? To put it another way, let's take Joda Time out of the equation... what would you expect the output of this to be?

您正在调用getMonthOfYear()- 只返回一个int. 比October这更能让您满意的一个月之前,它可能返回什么价值?换句话说,让我们把 Joda Time 排除在外……你希望它的输出是什么?

int month = 9;
System.out.println(" Month : " + month);

?

?

You need to understand the difference between data(an integer in this case) and the textual representation you want for that integer. If you want a specific format, I suggest you use DateTimeFormatter. (It's very rarely a good idea to print out a single field at a time anyway... I would have expected you to want something like "2013-09-08" as a single string.)

您需要了解数据(在本例中为整数)与您想要的该整数的文本表示之间的区别。如果你想要一个特定的格式,我建议你使用DateTimeFormatter. (无论如何,一次打印一个字段很少是一个好主意......我本来希望你想要像“2013-09-08”这样的东西作为单个字符串。)

You could also use String.formatto control the output format, or DecimalFormat, or PrintStream.printf- there are any number of ways of formatting integers. You need to understand that the number 9 is justthe number 9 though - it doesn't have a format associated with it.

您还可以使用String.format来控制输出格式,或DecimalFormat, 或PrintStream.printf- 有多种格式化整数的方法。您需要了解数字 9只是数字 9 - 它没有与之关联的格式。

回答by Reimeus

You can simply use AbstractDateTime#toString(String)

你可以简单地使用 AbstractDateTime#toString(String)

System.out.println(" Month : "+ dateTime.toString("MM"));
System.out.println(" Day : "+ dateTime.toString("dd")); 

回答by Silvano Silva

Example:

例子:

Calendar c = Calendar.getInstance();
System.out.format("%tB %te, %tY%n", c, c, c); // -->  "September 10, 2013"
System.out.format("%tl:%tM %tp%n", c, c, c);  // -->  "01:59 pm"
System.out.format("%tD%n", c);    // -->  "09/10/13"