내 앱에서 이번 주에 사용자가 가지고있는 약속 목록을 표시하고 싶습니다. API에 전달할 인수는 fromDate 및 toDate (밀리 초)입니다. 나는 그 주 마지막 날의 밀리 세컨드 단위로 날짜를 얻는 법을 배워야한다. (주일은 일요일부터 시작하여 토요일까지 끝난다.)다음 토요일의 날짜를 밀리 초 (long)로 가져옵니다.
0
A
답변
0
// get today and clear time of day
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);
// get start of this week in milliseconds
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
System.out.println("Start of this week: " + cal.getTime());
System.out.println("... in milliseconds: " + cal.getTimeInMillis());
// start of the next week
cal.add(Calendar.WEEK_OF_YEAR, 1);
System.out.println("Start of the next week: " + cal.getTime());
System.out.println("... in milliseconds: " + cal.getTimeInMillis());
주말 마지막 날에 6을 더하십시오 !!
0
일주일의 첫 번째 요일을 일요일로 추가 한 다음 6 일을 더하면됩니다.
// Get current time. You can reset hours/ minutes/ seconds it with default values which you want.
Calendar c = new GregorianCalendar(Locale.getDefault());
// Set first day of week
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
// Add 6 into that to get Saurday
c.add(Calendar.DAY_OF_WEEK, 6);
// Get millisenconds for Saturday
long millies = c.getTimeInMillis();
0
다가오는 토요일을 밀리 초 단위로 확인하려면 아래 코드를 확인하십시오.
public class NextSaturdayMain
{
public static void main(String[] args)
{
System.out.println(getSaturday(new Date()));
}
public static long getSaturday(Date today)
{
Calendar cal = Calendar.getInstance();
cal.setTime(today);
int dow = cal.get(Calendar.DAY_OF_WEEK);
while (dow != Calendar.SATURDAY) {
int date = cal.get(Calendar.DATE);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
if (date == getMonthLastDate(month, year)) {
if (month == Calendar.DECEMBER) {
month = Calendar.JANUARY;
cal.set(Calendar.YEAR, year + 1);
} else {
month++;
}
cal.set(Calendar.MONTH, month);
date = 1;
} else {
date++;
}
cal.set(Calendar.DATE, date);
dow = cal.get(Calendar.DAY_OF_WEEK);
}
System.out.println(cal.getTime());
return cal.getTimeInMillis();
}
private static int getMonthLastDate(int month, int year)
{
switch (month)
{
case Calendar.JANUARY:
case Calendar.MARCH:
case Calendar.MAY:
case Calendar.JULY:
case Calendar.AUGUST:
case Calendar.OCTOBER:
case Calendar.DECEMBER:
return 31;
case Calendar.APRIL:
case Calendar.JUNE:
case Calendar.SEPTEMBER:
case Calendar.NOVEMBER:
return 30;
default: // Calendar.FEBRUARY
return year % 4 == 0 ? 29 : 28;
}
}
}
+2
여기에 게시 된 것 이상을 추가하지 않아 귀하의 웹 사이트 링크를 제거했습니다. [자신의 블로그를 홍보하는 것이 허용됩니까?] (https://meta.stackoverflow.com/questions/273209/is-it-acceptable-to-promote-your-own-blog) 및 [하지 않는 방법]을 읽어보십시오. 스패머] (https://stackoverflow.com/help/promotion) – adiga
어떤 국가에서 사용할 수 있습니까? –
@MehulKanzariya 예. Locale.getDefault()를 사용하면됩니다. 그리고 특정 국가를 원할 경우 'Locale.US'또는 'Locale.CANADA'등을 사용할 수 있습니다. –