4

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?

5
  • 2
    Day in which time zone? It's Wednesday for me right now, but in Australia it's already Thursday... (You probably want to use Calendar - or better yet, Joda Time...)
    – Jon Skeet
    Jul 2, 2014 at 20:28
  • 1
    Why don't you just use Calendar?
    – aProperFox
    Jul 2, 2014 at 20:29
  • @TylerOlson I couldnt find how to initialize calendar with a perviously known timestamp!
    – Dumbo
    Jul 2, 2014 at 20:32
  • @JonSkeet yes master!
    – Dumbo
    Jul 2, 2014 at 20:33
  • 1
    @Sean87: Look at Calendar.setTimeInMillis. But you still need to think about time zones...
    – Jon Skeet
    Jul 2, 2014 at 20:34

4 Answers 4

11

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;
2

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

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.