我有以下代码:
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
Run Code Online (Sandbox Code Playgroud)
有没有什么办法可以使用Joda API检索2位数的月份和年份的值.
Rei*_*eus 36
您可以简单地使用 AbstractDateTime#toString(String)
System.out.println(" Month : "+ dateTime.toString("MM"));
System.out.println(" Day : "+ dateTime.toString("dd"));
Run Code Online (Sandbox Code Playgroud)
upo*_*pog 19
An alternative way would be using decimal formater
DecimalFormat df = new DecimalFormat("00");
Run Code Online (Sandbox Code Playgroud)
==========================================================================================
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());
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18032 次 |
| 最近记录: |