(Android) Get first day of week

前端 未结 4 1784
长发绾君心
长发绾君心 2020-12-21 15:05

How to get first day of week, when we know one of the date in the week?

I know the current month , the current year and the current date of week. Then, are there so

相关标签:
4条回答
  • 2020-12-21 15:50

    hello if you need to get the first day of the current week "monday"

    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_MONTH,  Calendar.MONDAY-calendar.get(Calendar.DAY_OF_WEEK));
    Log.w(TAG, "first day of week" + calendar.getTime());
    

    2 if you need the first day of the week for 20-12-2016

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_MONTH, 20);
    calendar.set(Calendar.MONTH,Calendar.DECEMBER);
    calendar.set(Calendar.YEAR,2016);
    calendar.add(Calendar.DAY_OF_MONTH,Calendar.MONDAY - calendar.get(Calendar.DAY_OF_WEEK));
    Log.w(TAG,"first day of week:"+calendar.getTime());
    

    i hope this will help you

    0 讨论(0)
  • 2020-12-21 15:53

    hello if you need to get the first day of the current week "monday"

    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_MONTH,  Calendar.MONDAY-calendar.get(Calendar.DAY_OF_WEEK));
      Log.w(TAG, "first day of week" + calendar.getTime());
    

    2 if you need the first day of the week for 20-12-2016

    Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, 20);
            calendar.set(Calendar.MONTH,Calendar.DECEMBER);
            calendar.set(Calendar.YEAR,2016);
            calendar.add(Calendar.DAY_OF_MONTH,Calendar.MONDAY - calendar.get(Calendar.DAY_OF_WEEK));
        Log.w(TAG,"first day of week:"+calendar.getTime());
    

    i hope this will help you

    0 讨论(0)
  • 2020-12-21 16:03

    Try this:

    // get today and clear time of day

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
    cal.clear(Calendar.MINUTE);
    cal.clear(Calendar.SECOND);
    cal.clear(Calendar.MILLISECOND);
    

    // get start of this week in milliseconds

    cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
    System.out.println("Start of this week:       " + cal.getTime());
    System.out.println("... in milliseconds:      " + cal.getTimeInMillis());
    

    Reference :How to get the first day of the current week and month?

    Hope this may help you!

    0 讨论(0)
  • 2020-12-21 16:04

    tl;dr

    The modern approach uses the java.time classes rather than the troublesome old Calendar & Date classes.

    LocalDate.of( 2017 , Month.MARCH , 23 )
             .with( TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) )
    

    LocalDate

    The LocalDate class represents a date-only value without time-of-day and without time zone.

    LocalDate someDate = LocalDate.of( 2017 , Month.MARCH , 23 ) ;
    

    TemporalAdjuster

    To get the first day of the week, (a) decide what is the first day of the week for you, (b) use a TemporalAdjuster implementation defined in TemporalAdjusters to get the date for a specific DayOfWeek enum object.

    LocalDate ld = someDate.with( TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) ) ;
    

    About java.time

    The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

    Where to obtain the java.time classes?

    • Java SE 8, Java SE 9, and later
      • Built-in.
      • Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
    • Java SE 6 and Java SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
      • See How to use ThreeTenABP….
    0 讨论(0)
提交回复
热议问题