Java 各種時間上的操作範例
分享一些在專案中用到JAVA與日期相關的操作,包括:
- 取得目前的年、月、日
- 判斷兩個日期的大小
- 計算兩個日期的差距
- 取得昨天的日期
- 取得上個月的開始與結束日
package CDIT.stanley; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.Calendar; public class dateOperation { public static int differentDays(Date date1,Date date2){ Calendar cal1 = Calendar.getInstance(); cal1.setTime(date1); Calendar cal2 = Calendar.getInstance(); cal2.setTime(date2); int day1= cal1.get(Calendar.DAY_OF_YEAR); int day2 = cal2.get(Calendar.DAY_OF_YEAR); int year1 = cal1.get(Calendar.YEAR); int year2 = cal2.get(Calendar.YEAR); if(year1 != year2){ int timeDistance = 0 ; for(int i = year1 ; i < year2 ; i ++){ if(i%4==0 && i%100!=0 || i%400==0){ timeDistance += 366; } else{ timeDistance += 365; } } return timeDistance + (day2-day1) ; } else{ return day2-day1; } } public static Date getFirstMonthDay(Calendar calendar) { calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE)); return calendar.getTime(); } public static Date getLastMonthDay(Calendar calendar) { calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE)); return calendar.getTime(); } public static void main(String[] args) throws ParseException { //取得目前的年、月、日 Calendar calendar = Calendar.getInstance(); System.out.println("今天是" + calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) + 1) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日"); System.out.println("=================================================="); //輸出:今天是2017年8月24日 //判斷兩個日期的大小 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.TAIWAN); Date date1 = sdf.parse("2017-08-23"); Date date2 = sdf.parse("2016-09-22"); System.out.println("Date1 < Date2 : " + date1.before(date2)); System.out.println("Date1 > Date2 : " + date1.after(date2)); System.out.println("=================================================="); //輸出:Date1 < Date2 : false、Date1 > Date2 : true //計算兩個日期的差距 System.out.println("Date1 & Date2 差距 : " + differentDays(date1 , date2) + "天"); System.out.println("=================================================="); //輸出:Date1 & Date2 差距 : 31天 //取得昨天的日期 calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -1); String yestedayDate = sdf.format(calendar.getTime()); System.out.println("昨天是" + yestedayDate); System.out.println("=================================================="); //輸出:昨天是2017-08-23 //取得上個月的開始與結束日 calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH,-1); String monthDayFirst = sdf.format(getFirstMonthDay(calendar)); String monthDayLast = sdf.format(getLastMonthDay(calendar)); System.out.println("上個月的第一天是" + monthDayFirst); System.out.println("上個月的最後一天是" + monthDayLast); System.out.println("=================================================="); //輸出:上個月的第一天是2017-07-01、上個月的最後一天是2017-07-31 } }
Как официально купить аттестат 11 класса с упрощенным обучением в Москве
Contemporary country could also be a bit sentimental, however it is not corny.
Returning to Eire in 1940, she labored for the Dublin retailer Richard Alan, remaining unknown to most people, for the following thirteen years, until she changed the French-Canadian head designer, Gaston Mallet in 1953.
бизнес франшизы бизнес франшизы .
Minard objected the question of Weinberger in regards to the brokers fees, to which Hanson sustained the motion.
Как приобрести диплом о среднем образовании в Москве и других городах
Maybe we need not choose.
франшизп франшизп .
Disinfectant carpet has been positioned on the entrance of the MPNR for visitors to disinfect the soles of their shoes.
June 8, 1923. p.
франшизы купить [url=https://www.franshizy18.ru]франшизы купить[/url] .
топ франшиз 2024 [url=www.franshizy19.ru/]топ франшиз 2024[/url] .
франшизы купить [url=www.franshizy22.ru]франшизы купить[/url] .
If this sounds intriguing to you, explore eclectic nation fashion.
Удивительно, но купить диплом кандидата наук оказалось не так сложно
rybpharm rybelsus: rybpharm canada – buy rybelsus rybpharm
I would like to add when you do not actually have an insurance policy or else you do not remain in any group insurance, chances are you’ll well gain from seeking the help of a health insurance professional. Self-employed or those that have medical conditions ordinarily seek the help of an health insurance brokerage. Thanks for your text.
I would like to thnkx for the efforts you have put in writing this web site. I am hoping the same high-grade site post from you in the upcoming also. Actually your creative writing abilities has inspired me to get my own site now. Really the blogging is spreading its wings rapidly. Your write up is a great example of it.
алкоголизм лечение вывод из запоя ростов [url=https://ideya.forums.party/viewtopic.php?id=653]https://ideya.forums.party/viewtopic.php?id=653[/url] .
William Sprague, Assistant Director of Marine, Hong Kong.
Edward Martin Furnival Jones, CBE.
For a fun, vintage contact, strive switching up the shade.
In accordance with the tradition, Gotama, earlier than reaching the state of Buddha, had been a Bodhisattva for many centuries.
The resin patches assist and protect the porous floor of this gemstone to maintain it for a longer time.
Although the United States was not in hazard of an existential assault, the national sense after Pearl Harbor was to use all of the nation’s resources to defeat Germany and Japan.
Peter Malcolm Shepherd, Chairman, Wool Trade Training Board.
франшиза в россии franshizy18.ru .
сколько стоит купить франшизу сколько стоит купить франшизу .
франшиза 2024 франшиза 2024 .
что можно открыть по франшизе что можно открыть по франшизе .