android如何识别一周中的同一天 [英] android how to identify the same days of a week

查看:174
本文介绍了android如何识别一周中的同一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要管理日期,我正在使用日历课程。我将时间戳保存在数据库中,使用

To manage dates I'm using the class Calendar. I'm saving timestamps in a DB, using

日历时间戳= Calendar.getInstance();

我能够使用长值存储日期来从数据库中保存和检索日期。

I'm able to save and retrieve the dates from the DB using long values to store dates.

这是我的问题:当我有一个要存储的时间戳,我需要知道数据库中是否已经存在属于同一周的时间戳。为此,我认为我可以使用以下两种方法:

This is my problem: when I have a timestamp to store, I need to know if in the DB there is already a timestamp belonging to the same week. To do this I thought I could use the couple of methods:

timestamp.get(Calendar.YEAR) timestamp.get (Calendar.WEEK_OF_YEAR)

唯一地标识一个星期,但是对于连续几天都属于连续几天的星期,情况并非如此。

to uniquely identify a week, but this isn't true for weeks having days belonging to two consecutive years.

这是我在2014年最后一周获得的示例。

This is an example of what I get for the last week of the year 2014.

12月27日,星期六,2014年
年:2014年
周:52

2014年12月28日,星期日
年: 2014
星期:1

2014年12月29日,星期一
年:2014
星期:1

2014年12月30日,星期二
年:2014
星期:1

2014年12月31日,星期三
年:2014年
星期:1

2015年1月1日,星期四
年:2015
星期:1

星期五, 2015年1月2日
年:2015年
周:1

星期六, 2015年1月3日
年:2015年
周:1

2015年1月4日,星期日
年:2015
周:2

从2014年12月28日到2015年3月1日之间的日期属于同一周(在java的这一周从星期日开始),但是他们有不同的年份,所以这对夫妇(YEAR,WEEK_OF_YEAR)没有给我他们属于同一周的信息。

The days from 28/12/2014 to 3/1/2015 belong to the same week (in java the week starts on Sunday), but they got a different year, so the couple (YEAR, WEEK_OF_YEAR) doesn't give me the info that they belong to the same week.

我该如何解决?

推荐答案

按照@basil的建议,我尝试了Joda-Time库,发现在那里解决我的问题。
要获取一年中的星期,我可以使用方法 getWeekOfWeekyear(),要获取年份,我可以使用方法 getWeekyear()

As suggested by @basil, I tried the Joda-Time library and I found there the solution to my problem. To get the week of the year I can use the method getWeekOfWeekyear() and to get the year I can use the method getWeekyear()

这样,这两个值唯一地标识了属于同一周的日期,该周从星期一开始,到星期日结束(与Java日历类相比,这是另一个附加值) 。

In this way, the couple of values uniquely identify the day belonging to the same week, with the week starting in Monday and ending in Sunday (another added value compared to the Java calendar class).

遵循相同的问题示例。

DateTime[] timestamp = new DateTime[10];

for(int i=0; i<10; i++){
   //set the start of the period
   DateTime time = new DateTime(2014, 12, 27, 18, 30, 50);
   //add i days to the 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);
}

结果是:

2014年12月27日,星期六| year.week = 2014.52

2014年12月28日,星期日| year.week = 2014.52

2014年12月29日,星期一| year.week = 2015.1

2014年12月30日,星期二| year.week = 2015.1

2014年12月31日,星期三| year.week = 2015.1

2015年1月1日,星期四| year.week = 2015.1

2015年1月3日,星期六| year.week = 2015.1

2015年1月4日,星期日| year.week = 2015.1

2015年1月5日,星期一| year.week = 2015.2

使用此库似乎还有很多其他优点,它将对我必须执行的其他计算很有用。

It looks like there are a lot of other advantages in using this library and it will be useful for other calculations I have to perform.

谢谢@basil。

这篇关于android如何识别一周中的同一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆