问题
I tried the below code but it gives me the name of the day of week two days ago.
DatePicker picker;
int date = picker.DayOfMonth;
int month = (picker.Month + 1);//month is 0 based
int year = picker.Year;
SimpleDateFormat simpledateformat = new SimpleDateFormat("EEE");
Date dt = new Date(year, month, date);
回答1:
First convert your Date in to specific Date format using SimpleDateFormat
Use SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE");
to get Day name in week
WHERE EEEE
-> Day name in week
SAMPLE CODE
SimpleDateFormat inFormat = new SimpleDateFormat("dd-MM-yyyy");
try {
Date myDate = inFormat.parse(date+"-"+month+"-"+year);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE");
String dayName=simpleDateFormat.format(myDate);
} catch (ParseException e) {
e.printStackTrace();
}
回答2:
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE", Locale.US);
String asWeek = dateFormat.format(dt);
回答3:
DateTimeFormatter dayOfWeekFormatter
= DateTimeFormatter.ofPattern("EEE", Locale.ENGLISH);
LocalDate date = LocalDate.of(
picker.getYear(), picker.getMonth(), picker.getDayOfMonth());
System.out.println(date.format(dayOfWeekFormatter));
Picking 2018-04-09 this printed
Mon
I am using and recommending java.time
, the modern Java date and time API. The Date
class is long outdated, and you are using a deprecated constructor. It was deprecated because it works unreliably across time zones, so you shouldn’t. SimpleDateFormat
is not only outdated, it is also notoriously troublesome. I recommend you avoid those classes altogether. The modern API is so much nicer to work with.
What went wrong in your code?
You’ve got two bugs apart from using the deprecated Date
constructor and the outdated classes:
- It’s the
Date
’s month that is 0-based (not that ofDatePicker
), so you need to subtract 1, not add 1 (or maybe they are both 0-based??). - The deprecated
Date
constructor’s year is “1900-based”. This may have seemed a good idea when the class was designed in the 1990’s: you could just specify 95 to get 1995. When you pass 2018 to the constructor, you get year 3918. That’s right. :-(
Question: Can I use java.time on Android?
Yes, java.time
works nicely on older and newer Android devices. It just requires at least Java 6.
- In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
- On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages.
Links
- Oracle tutorial: Date Time explaining how to use
java.time
. - Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
回答4:
Calendar calendar = Calendar.getInstance();
DateFormat date4= new SimpleDateFormat("EEEE", Locale.getDefault());
String localTime4 = date4.format(calendar.getTime());
Simple and easy way just use this
来源:https://stackoverflow.com/questions/49725492/how-to-get-day-of-week-name-from-selected-date-month-and-year-in-android