java - android how to identify the same days of a week -


to manage dates i'm using class calendar. i'm saving timestamps in db, using

calendar timestamp = calendar.getinstance();

i'm able save , retrieve dates db using long values store dates.

this problem: when have timestamp store, need know if in db there timestamp belonging same week. thought use couple of methods:

timestamp.get(calendar.year) , timestamp.get(calendar.week_of_year)

to uniquely identify week, isn't true weeks having days belonging 2 consecutive years.

this example of last week of year 2014.

saturday, december 27, 2014 year: 2014 week: 52

sunday, december 28, 2014 year: 2014 week: 1

monday, december 29, 2014 year: 2014 week: 1

tuesday, december 30, 2014 year: 2014 week: 1

wednesday, december 31, 2014 year: 2014 week: 1

thursday, january 1, 2015 year: 2015 week: 1

friday, january 2, 2015 year: 2015 week: 1

saturday, january 3, 2015 year: 2015 week: 1

sunday, january 4, 2015 year: 2015 week: 2

the days 28/12/2014 3/1/2015 belong same week (in java week starts on sunday), got different year, couple (year, week_of_year) doesn't give me info belong same week.

how can solve?

as suggested @basil, tried joda-time library , found there solution problem. week of year can use method getweekofweekyear() , year can use method getweekyear()

in way, couple of values uniquely identify day belonging same week, week starting in monday , ending in sunday (another added value compared java calendar class).

following same example of question.

datetime[] timestamp = new datetime[10];  for(int i=0; i<10; i++){    //set start of period    datetime time = new datetime(2014, 12, 27, 18, 30, 50);    //add days start date    timestamp[i] = time.plus(period.days(i));     int weekyear = timestamp[i].getweekyear();    int weekofweekyear = timestamp[i].getweekofweekyear();     log.d(constants.log_tag, timestamp[i].tostring("eeee, mmmm d, yyyy") + " | year.week = " + weekyear + "." + weekofweekyear); } 

the result is:

saturday, december 27, 2014 | year.week = 2014.52

sunday, december 28, 2014 | year.week = 2014.52

monday, december 29, 2014 | year.week = 2015.1

tuesday, december 30, 2014 | year.week = 2015.1

wednesday, december 31, 2014 | year.week = 2015.1

thursday, january 1, 2015 | year.week = 2015.1

saturday, january 3, 2015 | year.week = 2015.1

sunday, january 4, 2015 | year.week = 2015.1

monday, january 5, 2015 | year.week = 2015.2

it looks there lot of other advantages in using library , useful other calculations have perform.

thank @basil.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -