Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jodatime

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.

like image 483
Shantanoo K Avatar asked Sep 10 '13 15:09

Shantanoo K


People also ask

How do I change the date format in Joda-time?

How to change the SimpleDateFormat to jodatime? String s = "2014-01-15T14:23:50.026"; DateTimeFormatter dtf = DateTimeFormat. forPattern("yyyy-MM-dd'T'HH:mm:ss. SSSS"); DateTime instant = dtf.

Is Joda-time deprecated?

So the short answer to your question is: YES (deprecated).

What is Joda-time format?

Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters. Mid level - pattern-based, like SimpleDateFormat.

Does Joda DateTime have time zones?

Adjusting Time ZoneUse the DateTimeZone class in Joda-Time to adjust to a desired time zone. Joda-Time uses immutable objects. So rather than change the time zone ("mutate"), we instantiate a new DateTime object based on the old but with the desired difference (some other time zone). Use proper time zone names.


1 Answers

You can simply use AbstractDateTime#toString(String)

System.out.println(" Month : "+ dateTime.toString("MM"));
System.out.println(" Day : "+ dateTime.toString("dd")); 
like image 84
Reimeus Avatar answered Sep 28 '22 05:09

Reimeus