• 10
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Calendar.set(..) not working on android 6.0

I need to set calendar to next week's monday. My code works on Android 9.0 but on Android 6.0 it works only while debugging.

Problem is with Calendar.set(..) functions, they just don't work. For example calendar.set(Calendar.WEEK_OF_YEAR, 17) won't change calendar week to 17, but when it is debugging it will change it to 17.

Here is my code:

       Calendar mcurrentTime = Calendar.getInstance(Locale.GERMANY);
        if(AppHelper.getInstance().getNextWeek() != 0){

        mcurrentTime.set(Calendar.WEEK_OF_YEAR, 
        AppHelper.getInstance().getNextWeek());
        mcurrentTime.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        }


  weekNumberTv.setText(mcurrentTime.get(Calendar.WEEK_OF_YEAR)+"");

Android 9.0 weekNumberTv shows 17

Android 6.0 weekNumberTv shows 16

If start debugging mode

Android 6.0 weekNumberTv shows 17

    • Sounds like different default week schemes. Do you always get 1 week too early? The Calendar class is poorly designed and long outdated. Use java.time, the modern Java date and time API, for simpler and more natural code or behavour that is easier to predict.

To do time calculations in versions prior to 7.0 sadly you will have to use JavaTime package or its backport.

Implement ThreeTen Android Backport library:

implementation 'com.jakewharton.threetenabp:threetenabp:1.2.0'

https://github.com/JakeWharton/ThreeTenABP

Then initialize it in onCreate method.

AndroidThreeTen.init(this)

Make sure that these libraries are included and not Java 8 ones.

import org.threeten.bp.LocalDate
import org.threeten.bp.temporal.ChronoUnit
import org.threeten.bp.temporal.WeekFields
import com.jakewharton.threetenabp.AndroidThreeTen

Code to finish work

var mCurrentTime = LocalDate.now()
val weekFields = WeekFields.of(Locale.GERMANY)

val currentDayOfWeek = mCurrentTime.get(weekFields.dayOfWeek())
//subtract day of week to monday
mCurrentTime=mCurrentTime.minus((currentDayOfWeek.toLong()-1),ChronoUnit.DAYS)
//add week starting from monday
mCurrentTime=mCurrentTime.plus(1,ChronoUnit.WEEKS)
//get weekOfYear
val weekOfCurrentTime=mCurrentTime.get(weekFields.weekOfYear())
show_week_in_year.text=weekOfCurrentTime.toString()

Sorry for Kotlin. Java is on vacation.

  • 1
Reply Report