我有一个格式化的时间列表,如下所示:
00:30:00 - 01:00:00 - 。 。 。 。 二十三时30分00秒
如何从Android中的该列表中获取currentTime-(1小时)和currentTime +(3小时)之间的范围?
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("GTC"));
long now = Calendar.getInstance().getTimeInMillis();
Log.i("time", ""+now);
timeTableAdapter = new TimeTableAdapter(
TimeTableAdvanced.this, R.layout.timetable_item,
timeTableList);
adapterCount = timeTableAdapter.getCount();
for (int i = 0; i < adapterCount; i++) {
String inputString = timeTableList.get(i)
.getDepartureTime();
Date dat;
try {
dat = df.parse(inputString);
if (dat.getTime() <= now+(3*3600000) && dat.getTime() >= now-3600000 ) {
View item = timeTableAdapter.getView(i, null,
null);
timetablelayout.addView(item);
View line = new View(TimeTableAdvanced.this);
line.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 1));
line.setBackgroundColor(getResources()
.getColor(R.color.black));
timetablelayout.addView(line);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
答案 0 :(得分:0)
我认为这里没有任何困难。
假设您有X =以毫秒为单位的当前时间
HOUR_IN_MILLIS = 3600000
Time A = X - 1 hrs = X - HOUR_IN_MILLIS millis
Time B = X + 3 hrs = X + (3 * HOUR_IN_MILLIS) millis
如果您需要半小时间隔的范围,则迭代
for (long i = A; i <= B; i =+ HOUR_IN_MILLIS/2) { //add time to list }
答案 1 :(得分:0)
我解决了我的问题。显然,我不仅要花时间而且还要花费几分钟来获取当前时间的毫秒数。这是解决方案:
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
String date = df.format(Calendar.getInstance().getTime());
Date now;
long nowtime = 0;
try {
now = df.parse(date);
nowtime=now.getTime();
Log.i("time", ""+now);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
timeTableAdapter = new TimeTableAdapter(
TimeTableAdvanced.this, R.layout.timetable_item,
timeTableList);
adapterCount = timeTableAdapter.getCount();
for (int i = 0; i < adapterCount; i++) {
String inputString = timeTableList.get(i)
.getDepartureTime();
Date dat;
try {
dat = df.parse(inputString);
Log.i("time1", ""+dat.getTime());
Log.i("time2", ""+nowtime);
if (dat.getTime() <= nowtime+(3*3600000) && dat.getTime() >= nowtime-3600000 ) {
View item = timeTableAdapter.getView(i, null,
null);
timetablelayout.addView(item);
View line = new View(TimeTableAdvanced.this);
line.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 1));
line.setBackgroundColor(getResources()
.getColor(R.color.black));
timetablelayout.addView(line);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}