我有这段代码:
Calendar date1 = Calendar.getInstance();
DateTime dateTime1 = new DateTime(date1.YEAR, date1.MONTH, date1.DAY_OF_MONTH, date1.HOUR_OF_DAY, date1.MINUTE, date1.SECOND);
我的问题是date1
返回日期01/02/0005(以1/2/5格式)。我读到了SimpleDateFormat,但它返回的值相同。
答案 0 :(得分:1)
尝试使用以下代码行来获取正确的日期: -
Calendar ci = Calendar.getInstance();
String AM_PM;
if(ci.get(Calendar.AM_PM)==0)
{
AM_PM ="AM";
}
else
{
AM_PM ="PM";
}
String CiDateTime = "" + ci.get(Calendar.YEAR) + "-" +
(ci.get(Calendar.MONTH) + 1) + "-" +
ci.get(Calendar.DAY_OF_MONTH)+" "+AM_PM;
System.out.println("time=========================================="+CiDateTime);
对于您的解决方案,请尝试这些
Calendar ci = Calendar.getInstance();
DateTime dateTime1 = new DateTime(ci.get(Calendar.YEAR), ci.get(Calendar.MONTH), ci.get(Calendar.DAY_OF_MONTH), ci.get(Calendar.HOUR_OF_DAY), ci.get(Calendar.MINUTE), ci.get(Calendar.SECOND);
答案 1 :(得分:1)
date1.YEAR
仅返回常量值。与Calendar.YEAR
相同的值。
相反,您需要将date1.get(...)
方法与Calendar.YEAR
结合使用。见这个例子:
Calendar date1 = Calendar.getInstance();
DateTime dateTime1 = new DateTime(date1.get(Calendar.YEAR), date1.get(Calendar.MONTH), date1.get(Calendar.DAY_OF_MONTH), date1.get(Calendar.HOUR_OF_DAY), date1.get(Calendar.MINUTE), date1.get(Calendar.SECOND);
答案 2 :(得分:-1)
尝试以下代码:
Calendar date1 = Calendar.getInstance();
Date date = new Date();
date1.setTime(date);
DateTime dateTime1 = new DateTime(date1.YEAR, date1.MONTH, date1.DAY_OF_MONTH, date1.HOUR_OF_DAY, date1.MINUTE, date1.SECOND);