Remove Leading “0” in day of month SimpleDateFormat

孤街醉人 提交于 2019-12-10 12:41:33

问题


Is it possible to remove the "0" in January 04, 2012?

I am currently using the following Java to get the date format like

Monday, January 04, 2012

I would like for it to look like

Monday, January 4, 2012

    Date anotherCurDate = new Date();

    SimpleDateFormat formatter = new SimpleDateFormat("EEEE', 'MMMM dd', ' yyyy");  
    final String formattedDateString = formatter.format(anotherCurDate);

 final TextView currentRoomDate = (TextView)  this.findViewById(R.id.CurrentDate); 

回答1:


SimpleDateFormat formatter = new SimpleDateFormat("EEEE', 'MMMM d', ' yyyy");

Should do it (one 'd' instead of two)?

http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html




回答2:


You can get a String without "0" in front of a number just by doing this :

Calendar today = Calendar.getInstance();
// If you want the day/month in String format  
String month = today.get(Calendar.MONTH)+""; 

// If you want the day/month in Integer format 
int monthInt = (int ) Integer.parseInt(month);


来源:https://stackoverflow.com/questions/8836294/remove-leading-0-in-day-of-month-simpledateformat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!