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 } }
лучшие капперы мира лучшие капперы мира .
лучшие капперы россии лучшие капперы россии .
buy semaglutide online: Buy compounded semaglutide online – rybelsus
prednisone in mexico: prednisone pharmacy – prednisone 40 mg tablet
Semaglutide pharmacy price: Semaglutide pharmacy price – Buy semaglutide pills
An attention-grabbing discussion is worth comment. I think that you should write more on this topic, it won’t be a taboo topic but typically people are not enough to speak on such topics. To the next. Cheers
I have learn a few just right stuff here. Definitely price bookmarking for revisiting. I surprise how a lot attempt you set to make this sort of magnificent informative site.
Thanks for your thoughts. One thing I’ve noticed is that banks in addition to financial institutions know the dimensions and spending behavior of consumers and also understand that most of the people max out their own credit cards around the breaks. They smartly take advantage of this kind of fact and then start flooding your own inbox plus snail-mail box having hundreds of no-interest APR credit card offers shortly after the holiday season concludes. Knowing that if you are like 98 of the American public, you’ll hop at the one opportunity to consolidate consumer credit card debt and switch balances to 0 rate credit cards.
You made some respectable factors there. I looked on the web for the issue and located most individuals will go along with with your website.
you will have a terrific weblog right here! would you prefer to make some invite posts on my weblog?
Thanks for the new stuff you have unveiled in your article. One thing I would like to touch upon is that FSBO human relationships are built eventually. By launching yourself to the owners the first few days their FSBO is definitely announced, ahead of masses start off calling on Friday, you produce a good network. By sending them tools, educational materials, free records, and forms, you become an ally. By subtracting a personal affinity for them plus their circumstances, you create a solid relationship that, most of the time, pays off in the event the owners decide to go with a broker they know as well as trust — preferably you actually.
Hello, i think that i noticed you visited my site so i came to ?go back the want?.I am trying to find issues to improve my web site!I guess its good enough to use some of your concepts!!
My partner and I absolutely love your blog and find most of your post’s to be just what I’m looking for. Does one offer guest writers to write content available for you? I wouldn’t mind producing a post or elaborating on a lot of the subjects you write regarding here. Again, awesome site!
I have figured out some new items from your web-site about pc’s. Another thing I’ve always assumed is that computers have become a product that each family must have for a lot of reasons. They offer convenient ways in which to organize households, pay bills, go shopping, study, listen to music and in some cases watch tv shows. An innovative approach to complete all of these tasks is with a notebook. These computer systems are mobile, small, effective and mobile.
I’m curious to find out what blog system you are working with? I’m having some minor security issues with my latest website and I’d like to find something more risk-free. Do you have any solutions?
https://prednisolone.pro/# prednisone 2.5 mg cost
Spot on with this write-up, I really think this web site needs way more consideration. I?ll most likely be once more to learn much more, thanks for that info.
Explore the benefits of decentralized asset swapping with the innovative and user-friendly platform provided by anyswap exchange
ventolin 200 mcg: where to get ventolin cheap – generic ventolin
I?ll right away snatch your rss feed as I can’t to find your e-mail subscription hyperlink or e-newsletter service. Do you have any? Kindly let me recognise in order that I may subscribe. Thanks.
ventolin prescription uk buy Ventolin where to buy ventolin
Buy compounded semaglutide online semaglutide Semaglutide pharmacy price
Thank you, I have recently been looking for information approximately this subject for a while and yours is the greatest I have found out so far. But, what in regards to the bottom line? Are you sure concerning the source?
Thank you for another great post. Where else could anyone get that kind of information in such an ideal way of writing? I’ve a presentation next week, and I am on the look for such information.
Buy compounded semaglutide online rybelsus generic cheap Rybelsus 14 mg
Just about all of the things you assert is supprisingly precise and it makes me wonder why I hadn’t looked at this in this light previously. This piece truly did switch the light on for me personally as far as this specific issue goes. But there is 1 issue I am not too cozy with so whilst I try to reconcile that with the core idea of your issue, permit me see what all the rest of your readers have to point out.Very well done.
neurontin online: neurontin 100 mg cost – neurontin over the counter
Great weblog here! Also your web site a lot up fast! What web host are you the use of? Can I am getting your associate link on your host? I desire my web site loaded up as quickly as yours lol
There may be noticeably a bundle to learn about this. I assume you made certain good factors in features also.
I believe one of your commercials caused my browser to resize, you might want to put that on your blacklist.