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 } }
Hi, Neat post. There’s a problem with your website in internet explorer, would check this? IE still is the market leader and a large portion of people will miss your fantastic writing due to this problem.
Pharmacie sans ordonnance: pharmacie en ligne – Pharmacie Internationale en ligne
https://pharmaciemeilleurprix.shop/# pharmacie en ligne sans ordonnance
pharmacie en ligne france livraison belgique
Viagra gГ©nГ©rique sans ordonnance en pharmacie: Viagra pharmacie – Prix du Viagra 100mg en France
pharmacie en ligne sans ordonnance Pharmacies en ligne certifiees pharmacie en ligne france livraison internationale
Achat mГ©dicament en ligne fiable: pharmacie en ligne – pharmacie en ligne pas cher
pharmacie en ligne france livraison internationale: kamagra pas cher – п»їpharmacie en ligne france
Why expanding the College Football Playoff worked – and what still needs to be fixed
порно анальный секс
Now that it’s all over and the Ohio State Buckeyes are the college football national champions, it can be definitively said: expanding the College Football Playoff worked.
The grand experiment to allow more teams to play for the national championship wasn’t perfect, but it ended up where it was supposed to: a worthy national champion with exciting, close games in the later rounds when the best teams faced one another. It gave us awesome scenes on campuses around the nation, created new legends and showed how a sport so steeped in tradition can evolve when faced with new demands from its fans and business partners.
Here are four reasons why the new version of the College Football Playoff worked – and the areas that can still be fixed.
The committee picked the right teams, even if some games were blowouts
Before the games kicked off in December, much of the focus was put on the inclusion of Southern Methodist University (SMU) and Indiana University – two teams that won a bunch of games but didn’t have the brand recognition of schools like Alabama, South Carolina and Ole Miss.
Here’s what else those teams had that SMU and Indiana didn’t: three losses.
The Hoosiers lost only once in the regular season – to eventual national champion Ohio State. The Mustangs had lost twice, once to Brigham Young University and again in the ACC championship game to Clemson.
In the first year of the expanded, 12-team playoff, could the committee really leave out a major conference team with 11 wins and punish another one for playing for a conference championship while other teams sat at home? Warde Manuel, the University of Michigan athletic director who served as chair of the committee, said they could not.
I appreciate, cause I found exactly what I was looking for. You’ve ended my four day long hunt! God Bless you man. Have a great day. Bye
https://pharmaciemeilleurprix.com/# pharmacie en ligne
pharmacie en ligne france livraison belgique
Terrific work! This is the type of info that are supposed to be shared across the net. Shame on the seek engines for no longer positioning this submit upper! Come on over and consult with my website . Thanks =)
vente de mГ©dicament en ligne Pharmacies en ligne certifiees vente de mГ©dicament en ligne
http://tadalafilmeilleurprix.com/# Pharmacie sans ordonnance
pharmacies en ligne certifiГ©es
It’s perfect time to make a few plans for the longer term and it is time to be happy. I have learn this submit and if I may just I wish to suggest you few interesting issues or advice. Perhaps you could write subsequent articles regarding this article. I want to learn more things about it!
naturally like your web site but you need to check the spelling on quite a few of your posts. Several of them are rife with spelling issues and I to find it very bothersome to tell the reality nevertheless I will definitely come back again.
Achat mГ©dicament en ligne fiable https://kamagrameilleurprix.shop/# pharmacie en ligne avec ordonnance
pharmacie en ligne: pharmacie en ligne fiable – pharmacies en ligne certifiГ©es
You actually make it appear really easy together with your presentation but I in finding this topic to be really one thing which I believe I’d never understand. It kind of feels too complicated and extremely huge for me. I’m looking forward on your subsequent post, I?ll try to get the hold of it!
Just wish to say your article is as astonishing. The clarity in your post is just nice and i could assume you are an expert on this subject. Well with your permission let me to grab your feed to keep updated with forthcoming post. Thanks a million and please continue the enjoyable work.
Achat mГ©dicament en ligne fiable: pharmacie en ligne france – pharmacie en ligne france pas cher
Pretty section of content. I simply stumbled upon your weblog and in accession capital to assert that I acquire actually enjoyed account your blog posts. Any way I?ll be subscribing to your augment or even I achievement you get admission to persistently fast.
Thanks again for the article.Really looking forward to read more. Really Great.
Pharmacie en ligne livraison Europe kamagra en ligne pharmacie en ligne france pas cher
I figured out more new stuff on this weight loss issue. Just one issue is a good nutrition is extremely vital if dieting. A tremendous reduction in bad foods, sugary ingredients, fried foods, sweet foods, pork, and white flour products could be necessary. Keeping wastes harmful bacteria, and toxins may prevent goals for losing weight. While a number of drugs temporarily solve the problem, the awful side effects are not worth it, and they also never provide more than a temporary solution. It can be a known idea that 95 of dietary fads fail. Thanks for sharing your thinking on this website.
I’ve been absent for a while, but now I remember why I used to love this blog. Thank you, I will try and check back more often. How frequently you update your web site?
Thanks for your post. I’d like to comment that the cost of car insurance differs from one insurance policy to another, for the reason that there are so many different issues which contribute to the overall cost. One example is, the brand name of the automobile will have a tremendous bearing on the purchase price. A reliable old family automobile will have a more economical premium when compared to a flashy racecar.
Howdy! I could have sworn I’ve been to this site before but after reading through some of the post I realized it’s new to me. Anyways, I’m definitely delighted I found it and I’ll be bookmarking and checking back frequently!
I like the valuable info you provide in your articles. I will bookmark your blog and check again here regularly. I am quite sure I will learn plenty of new stuff right here! Best of luck for the next!
One other thing is that an online business administration diploma is designed for people to be able to efficiently proceed to bachelor’s degree courses. The Ninety credit education meets the other bachelor education requirements and when you earn the associate of arts in BA online, you’ll have access to the modern technologies in this particular field. Several reasons why students want to get their associate degree in business is because they can be interested in this area and want to have the general education and learning necessary previous to jumping right bachelor college diploma program. Many thanks for the tips you really provide in your blog.
SildГ©nafil Teva 100 mg acheter: Acheter Viagra Cialis sans ordonnance – Le gГ©nГ©rique de Viagra