Java Android - 将日期格式化为“日、月 dd、yyyy”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19928757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - format Date to "Day, Month dd, yyyy"
提问by Christos Baziotis
I get from the user, the date he sets in a DatePickerDialog. The date i get is in this format:
我从用户那里得到了他在 DatePickerDialog 中设置的日期。我得到的日期是这种格式:
int selectedYear, int selectedMonth, int selectedDay
Can i format it to be like this "Day, Month dd, yyyy" as shown in the picture below?
我可以将其格式化为如下图所示的“日、月 dd、yyyy”吗?
采纳答案by dafi
Using the values returned from picker
使用选择器返回的值
int selectedYear = 2013;
int selectedDay = 20;
int selectedMonth = 11;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, selectedYear);
cal.set(Calendar.DAY_OF_MONTH, selectedDay);
cal.set(Calendar.MONTH, selectedMonth);
String format = new SimpleDateFormat("E, MMM d, yyyy").format(cal.getTime());
回答by Subramanian Ramsundaram
You can Try This:
你可以试试这个:
SimpleDateFormat sdf = new SimpleDateFormat("EEE-dd-MM-yyyy"); // Set your date format
String currentData = sdf.format(your actual date); // Get Date String according to date format
Here you can see details and all supported format:
您可以在此处查看详细信息和所有支持的格式:
http://developer.android.com/reference/java/text/SimpleDateFormat.html
http://developer.android.com/reference/java/text/SimpleDateFormat.html
回答by Baschi
Use a SimpleDateFormat
to format the date:
使用 aSimpleDateFormat
来格式化日期:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, selectedYear);
cal.set(Calendar.MONTH, selectedMonth);
cal.set(Calendar.DAY_OF_MONTH, selectedDay);
SimpleDateFormat sdf = new SimpleDateFormat();
String DATE_FORMAT = "EE, MMM dd, yyyy";
sdf.applyPattern(DATE_FORMAT);
String formattedDate = sdf.format(cal.getTime());