2013年9月15日星期日

java get the current system 24 hours before the time " when " array

Hello
java to get the current system time prior to 24 hours , " time" arrays such as the current system is the afternoon 16:00 to 17:00 yesterday afternoon, exactly 24 hours
Example:
[ 17 18 19 20 21 2,223,000,102,030,405 0,607,080,910,111,213 ; 14 15 16 ]
get such a total of 24 hours is dynamic if the time changes along with the corresponding data also changes .

Thank you god
------ Solution --------------------------- -----------------

Calendar c=Calendar.getInstance();
for(int i=0;i<24;i++){//当前时间循环24小时减下去
   System.out.println(c.get(Calendar.HOUR_OF_DAY));
   c.add(Calendar.HOUR_OF_DAY, -i);
}

------ Solution ----------------- ---------------------------
Date date = new Date();
int nowHour = date.getHours();
int[] hours = new int[24];
for (int i = 0; i < hours.length; i++) {
hours[i] = (nowHour++)%24;
}
for (int j = 0; j < hours.length; j++) {
System.out.println(hours[j]);
}

------ Solution ------- -------------------------------------
Date date = new Date();
// System.out.println(date);
Calendar c = new GregorianCalendar();
c.setTime(date);//给Calendar对象设置时间
int hour = c.get(Calendar.HOUR_OF_DAY);//取得当前时间的时数

---- - Solution --------------------------------------------
import java.util.Calendar;


public class Get24Hours {

public static void main(String[] args) {
Integer[] hours=new Integer[24];
for (int i = 0; i < 24; i++) {
Calendar calendar=Calendar.getInstance();
 calendar.add(Calendar.HOUR_OF_DAY, i);
 hours[i]=calendar.get(Calendar.HOUR_OF_DAY);
}
for (int i = 0; i < hours.length; i++) {
System.out.print(hours[i]+",");
}
}
}

没有评论:

发表评论