I am currently working on creating a custom date format such that it is supposed to show as "Monday, April 8, 2019 " but whenever I pick from dialog and apply simple date format, it returns the date with abbreviated month and time (which I don't need). any idea how I can update my code to get it to work in the above format? Here's my code :
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
String date = (++monthOfYear)+" "+dayOfMonth+", "+year;
SimpleDateFormat dateFormat = new SimpleDateFormat("E MMM dd yyyy");
dateFormat.format(new Date());
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myDate.setText(convertedDate.toString());
}
To achieve such a format: Monday, April 8, 2019
you have to format the date like this (source) :
SimpleDateFormat mDateFormat = new SimpleDateFormat("EEEE, MMMMM dd, yyyy");
myDate.setText(mDateFormat.format(convertedDate));
Good luck
I would do something like this:
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
Calendar mDate = Calendar.getInstance();
mDate.set(year, monthOfYear, dayOfMonth);
SimpleDateFormat mDateFormat = new SimpleDateFormat("EEEE, MMMMM dd, yyyy");
myDate.setText(mDateFormat.format(mDate.getTime()));
}
DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
.withLocale(Locale.US);
LocalDate date = LocalDate.of(year, ++monthOfYear, dayOfMonth);
String formattedDate = date.format(dateFormatter);
Monday, April 8, 2019
Don't bother with defining you own date format through a format pattern string. The format you want is already built in.
I am using java.time, the modern Java date and time API. The date/time clases that you tried to use, SimpleDateFormat
and Date
, are long outdated and poorly designed, the former in particular notoriously troublesome, so I didn't want to use those.
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6 .
org.threeten.bp
with subpackages. year
, monthOfYear
and dayOfMonth
into a string that you are trying to parse, which is the detour, but worse, the string hasn't got format E MMM dd yyyy
, so parsing it with your SimpleDateFormat
probably failed. If you haven't seen the stacktrace from e.printStackTrace();
, I suggest that you've got a serious problem in your project setup that you should fix before writing another code line. If you don't see the errors that happen in your code, you are coding blindfolded. The parse error caused the current date obtained from new Date()
to be displayed instead. convertedDate.toString()
returns a string in the format EEE MMM dd HH:mm:ss zzz yyyy
to be displayed. For example Tue Apr 09 09:39:47 EDT 2019
. Date.toString
always returns this format. It has nothing to do with the format the date was parsed from (even if it had been successfully parsed) since a Date
is just a point in time, it cannot have a format in it. java.time
was first described. java.time
to Java 6 and 7 (ThreeTen for JSR-310).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.