2012-04-22 90 views
0

我想以2012年4月22日格式显示日期。 我使用这个代码:以字符串格式获取月份

 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); 

     sdf.applyPattern("dd MMM yyyy"); 
     Date x = new Date(time.year,time.month,time.monthday); 
     System.out.println(sdf.format(x)); 

但我正在逐渐O/P为: 04月22日3912 我想知道为什么它显示3912代替2012年

+0

使用dd-MM-yyyy。我认为问题是因为分号 – Habib 2012-04-22 18:36:12

回答

1

请阅读api为Date类。它从1900年开始,所以在这个构造函数中你必须提供日期 - 1900.但是这个构造函数已被弃用,所以我的建议是开始使用Calendar对象来处理日期相关的操作。 为Java.da

+0

呃,对于Java.da是什么? – 2012-12-08 23:08:09

1
Calendar cal=Calendar.getInstance(); 
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy"); 
String dateresult = sdf.format(cal.getTime()); 
1
SimpleDateFormat dateformate = new SimpleDateFormat("dd-MMM-yyyy"); 
    Calendar currentDate = Calendar.getInstance(); 

    String currentDateStr = dateformate.format(currentDate.getTime()); 
    Log.i("Infoe ", " " + currentDateStr.toString());  
    System.out.println("Current date is : " + currentDateStr); 
    ////=== OR 

    SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy"); 

    sdf.applyPattern("dd-MMM-yyyy"); 
    Date x = new Date(currentDate.get(Calendar.YEAR) - 1900, 
      currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DAY_OF_MONTH)); 
    Log.i("Infoe ", " " + sdf.format(x)); 
    System.out.println(sdf.format(x)); 

其3912 BCZ ...日期设置好的像... Calendar.set(年+ 1900年,月,日)或GregorianCalendar的(年+ 1900年,月,日期)。每GregorianCalendar的

,因为我认为使用日历为Gd的日期已被弃用... 次日期格式使用DD-MMM-YYYY没有DD-MM-YYYY BCZ你想一个月前2个字符... For date Format

相关问题