Last active
March 10, 2019 12:21
-
-
Save ezhov-da/1dde39b14c343c6d30e134c02d8fc860 to your computer and use it in GitHub Desktop.
java номер недели
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://javatalks.ru/topics/34765 | |
<pre> | |
По ISO 8601, первой неделей в году считается та, которая содержит минимум 4 дня нового года (или, другими словами, которая содержит первый четверг года). | |
calendar.setMinimalDaysInFirstWeek(4); | |
У нас не совпадает номер недели, для этого необходимо указать. что новая неделя начинается только если попадает 4 последних дня на новый год. | |
</pre> | |
[code:]java[:code] | |
import java.util.Calendar; | |
import java.util.GregorianCalendar; | |
/** | |
* | |
* @author ezhov_da | |
*/ | |
public class TestNumWeek | |
{ | |
public static void main(String[] args) | |
{ | |
Calendar calendar = GregorianCalendar.getInstance(); | |
calendar.setMinimalDaysInFirstWeek(4); | |
calendar.set(Calendar.DAY_OF_MONTH, 1); | |
calendar.set(Calendar.MONTH, 0); | |
calendar.set(Calendar.YEAR, 2016); | |
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); | |
System.out.println(calendar.getTime()); | |
calendar.set(Calendar.DAY_OF_MONTH, 31); | |
calendar.set(Calendar.MONTH, 11); | |
calendar.set(Calendar.YEAR, 2015); | |
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); | |
System.out.println(calendar.getTime()); | |
calendar.set(Calendar.DAY_OF_MONTH, 27); | |
calendar.set(Calendar.MONTH, 11); | |
calendar.set(Calendar.YEAR, 2015); | |
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); | |
System.out.println(calendar.getTime()); | |
} | |
} | |
[/code] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment