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 } }
One thing is that one of the most widespread incentives for making use of your credit cards is a cash-back or perhaps rebate offer. Generally, you’ll have access to 1-5 back with various expenditures. Depending on the cards, you may get 1 back again on most buying, and 5 back again on expenditures made from convenience stores, gasoline stations, grocery stores as well as ‘member merchants’.
I think other web site proprietors should take this web site as an model, very clean and excellent user friendly style and design, as well as the content. You’re an expert in this topic!
Petronas reported 2022 revenues of RM375.Three billion.
The company regards a choose interval of the brand new Musical Express chart (only from 1952 to 1960) and the Document Retailer chart from 1960 to 1969 as predecessors for the interval up to eleven February 1969, the place multiples of competing charts (none official) coexisted aspect by aspect.
Union University sustained heavy injury and was closed till February 18.
This option was fairly costly as each telematics provider had totally different procedures for accessing and retrieving the data and the data format varied from provider to supplier.
Both the GT and SS have been created below venture code XP-797.
лучшие капперы с бесплатными прогнозами лучшие капперы с бесплатными прогнозами .
Surgutneftegas reported 2018 revenues of ₽1.555 trillion.
Meredith, Sam (19 March 2020).
The industry memos acknowledge that automobiles “are by far the best sources of air pollution” and in addition that air pollution causes adversarial well being effects and lodges toxins, including carcinogens, “deep into the lungs which would in any other case be eliminated within the throat”.
Since from past ten years consecutive five LIBOR rose rallies along with the US Dollars rise to 3 times as recommended by the LIBOR that we are nonetheless hoping for some great news from the US Dollar ends this year also.
Canadian Medical Association Journal.
Nevertheless, the Americanized Capri had just a few drawbacks.
Muthiah, S. (21 Could 2018).
Eliyahu M. Goldratt developed the speculation of Constraints partially to address the associated fee-accounting issues in what he calls the “cost world.” He offers a substitute, known as throughput accounting, that makes use of throughput (money for items bought to customers) in place of output (goods produced which will promote or may increase stock) and considers labor as a hard and fast quite than as a variable value.
Each had been vital excessive-revenue assets in a market gone mad for trucks, and Dearborn gave them yearly enhancements to protect their class-leading gross sales standing.
для вывода из запоя в стационаре http://www.vyvod-iz-zapoya-v-stacionare13.ru .
Thanks a lot for your post. I’d like to comment that the tariff of car insurance varies widely from one insurance plan to another, for the reason that there are so many different facets which play a role in the overall cost. For example, the model and make of the car will have a tremendous bearing on the price tag. A reliable outdated family car will have a less expensive premium than just a flashy expensive car.
Journal of European Integration.
One other issue is that if you are in a problem where you will not have a cosigner then you may really want to try to make use of all of your educational funding options. You will discover many awards and other scholarships and grants that will present you with funds to assist with university expenses. Many thanks for the post.
I cherished as much as you’ll receive carried out right here. The cartoon is attractive, your authored subject matter stylish. nonetheless, you command get bought an shakiness over that you wish be turning in the following. in poor health indubitably come more earlier again as precisely the similar just about very ceaselessly within case you defend this hike.
I discovered your blog website on google and verify just a few of your early posts. Continue to keep up the superb operate. I simply additional up your RSS feed to my MSN Information Reader. Looking for forward to reading extra from you afterward!?
Thanks for another informative blog. Where else could I get that type of info written in such a perfect way? I have a project that I’m just now working on, and I have been on the look out for such info.
Thanks for discussing your ideas. Another thing is that students have a selection between fed student loan and also a private student loan where it can be easier to go for student loan online debt consolidation than in the federal student loan.
It is really a nice and helpful piece of info. I?m glad that you shared this helpful information with us. Please keep us informed like this. Thanks for sharing.
I’ve been absent for some time, but now I remember why I used to love this blog. Thanks , I?ll try and check back more often. How frequently you update your web site?
I’m not sure exactly why but this web site is loading incredibly slow for me. Is anyone else having this problem or is it a problem on my end? I’ll check back later and see if the problem still exists.
What?s Taking place i am new to this, I stumbled upon this I’ve found It absolutely useful and it has helped me out loads. I’m hoping to contribute & aid other users like its helped me. Great job.
One thing is that often one of the most prevalent incentives for utilizing your credit cards is a cash-back and also rebate provision. Generally, you will get 1-5 back for various expenditures. Depending on the credit card, you may get 1 back again on most expenses, and 5 back again on expenses made going to convenience stores, filling stations, grocery stores plus ‘member merchants’.