Get name of day from timestamp in Android

余生长醉 提交于 2019-12-11 01:56:19

问题


I have a class that when its initialized, it records the time of initialization in a private field with a public getter:

public class TestClass {

   private long mTimestamp;

    public TestClass(){
      mTimestamp = System.getCurrentMillis();
    }

    public long getTimestamp(){
          return mTimestamp;
    }
}

I also have an enum with the name of days:

public enum Days implements Serializable {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
}

Now the problem is in another class I have to get the timestamp and set a Days field to the day that the class was initialized:

public class OtherClass {

     public Days getDayOfInitialization(TestClass testClass){
          //how to do this?
          Date date = new Date(testClass.getTimestamp())
          Days day = Date.getDay(); //Deprecated!
          //convert to enum and return...
     }
}

The getDay() method of Date is deprecated...how should I do this?


回答1:


Standard Week

You do not need that Enum as the standard for date-time (ISO 8601) defines a week as Monday to Sunday.

Joda-Time defines constants for each day-of-week name in English such as DateTimeConstants.MONDAY.

Avoid java.util.Date

You should be using Joda-Time library, as the java.util.Date and .Calendar classes are notoriously troublesome. Joda-Time works in Android according to others.

Avoid Milliseconds As Date-Time

Tracking date-time as milliseconds is less than optimal. Serializing to a ISO 8601 string is preferable. But if you must, so be it.

Time Zone Is Crucial

Time zone is crucial. Day-of-week is defined by time zone, as the comments above discussed.

If you want UTC, Joda-Time provides the constant DateTimeZone.UTC.

Joda-Time

DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
DateTime dateTime = new DateTime( millisSinceEpochInUtc, timeZone );
int dayOfWeekNumber = dateTime.getDayOfWeek(); // ISO 8601 standard says Monday is 1.
DateTimeFormatter formatter = DateTimeFormat.forPattern( "EEEE" ).withLocale( java.util.Locale.ENGLISH );
String dayOfWeekName = formatter.print( dateTime );



回答2:


If you just need the current day of week in a human-readable format, formatted to the current user's locale, then you could use this:

SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
String dayString = sdf.format(new Date());

If their locale is "en_US", the output is:

Wednesday

If their locale is "de_DE", the output is:

Mittwoch

If their locale is "fr_FR", the output is:

mercredi

But if you need a numerical representation of the day of week (for example, you wanted to get '1' if it is Sunday or '2' if it is Monday), then you could use Calendar:

Calendar cal = Calendar.getInstance();
int dayInt = cal.get(Calendar.DAY_OF_WEEK);



回答3:


use Calendar:

  long timeStamp = testClass.getTimestamp();
  Calendar c = Calendar.getInstance();
  c.setTimeInMillis(timeStamp);

  int dayNum = c.get(Calendar.DAY_OF_WEEK);

  Days day = Days.values()[dayNum];

  return day;



回答4:


You should use Calendar.getInstance() method, which provide a calendar based on System Settings' TimeZone

Calendar now = Calendar.getInstance();

Then get the day of the week as an int (1 = Sunday, 2 = Monday, and so on...)

int day = now.get(Calendar.DAY_OF_WEEK);


来源:https://stackoverflow.com/questions/24540443/get-name-of-day-from-timestamp-in-android

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