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 } }
I believe one of your commercials caused my web browser to resize, you might want to put that on your blacklist.
Music started playing as soon as I opened up this internet site, so frustrating!
Hello my friend! I wish to say that this article is awesome, nice written and include almost all vital infos. I would like to look extra posts like this .
Things i have always told people today is that while looking for a good online electronics shop, there are a few aspects that you have to factor in. First and foremost, you would like to make sure to choose a reputable and reliable retail store that has gotten great assessments and ratings from other people and industry leaders. This will make sure that you are getting through with a well-known store to provide good assistance and support to it’s patrons. Many thanks sharing your thinking on this blog.
Hi! I could have sworn I’ve been to this website 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 often!
http://denemebonusuverensiteler.win/# deneme bonusu veren siteler
slot siteleri guvenilir: slot siteleri 2024 – slot siteleri 2024
en iyi slot siteleri 2024: oyun siteleri slot – slot casino siteleri
Good day! I simply wish to give an enormous thumbs up for the nice information you could have right here on this post. I shall be coming again to your weblog for extra soon.
I have witnessed that wise real estate agents all over the place are Promoting. They are acknowledging that it’s more than just placing a sign post in the front property. It’s really with regards to building associations with these suppliers who one of these days will become purchasers. So, after you give your time and energy to aiding these traders go it alone : the “Law connected with Reciprocity” kicks in. Good blog post.
I have noticed that good real estate agents just about everywhere are Promoting. They are knowing that it’s in addition to placing a sign post in the front property. It’s really about building interactions with these vendors who later will become buyers. So, once you give your time and effort to helping these traders go it alone : the “Law regarding Reciprocity” kicks in. Good blog post.
Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your website? My website is in the very same niche as yours and my visitors would really benefit from a lot of the information you provide here. Please let me know if this okay with you. Thank you!
Hiya, I’m really glad I have found this info. Today bloggers publish just about gossips and net and this is actually annoying. A good site with exciting content, that’s what I need. Thank you for keeping this site, I will be visiting it. Do you do newsletters? Can not find it.
One thing I would like to say is the fact car insurance cancelling is a feared experience and if you’re doing the suitable things as a driver you simply won’t get one. Many people do are sent the notice that they have been officially dropped by their particular insurance company and many have to scramble to get more insurance from a cancellation. Low-cost auto insurance rates are usually hard to get after having a cancellation. Understanding the main reasons with regard to auto insurance cancellations can help drivers prevent losing one of the most essential privileges accessible. Thanks for the concepts shared through your blog.
Thanks a lot for the helpful content. It is also my belief that mesothelioma cancer has an extremely long latency period, which means that warning signs of the disease may well not emerge right until 30 to 50 years after the initial exposure to asbestos fiber. Pleural mesothelioma, which can be the most common kind and has an effect on the area within the lungs, could cause shortness of breath, breasts pains, and also a persistent coughing, which may bring on coughing up maintain.
Attractive portion of content. I simply stumbled upon your web site and in accession capital to say that I get actually loved account your blog posts. Any way I will be subscribing for your feeds and even I fulfillment you get entry to consistently quickly.
you are really a good webmaster. The website loading speed is amazing. It seems that you are doing any unique trick. Furthermore, The contents are masterpiece. you’ve done a fantastic job on this topic!
Thanks for your write-up on this blog site. From my own personal experience, there are occassions when softening way up a photograph could provide the digital photographer with an amount of an artsy flare. Often times however, the soft blur isn’t precisely what you had at heart and can usually spoil an otherwise good snapshot, especially if you anticipate enlarging that.
One more thing to say is that an online business administration course is designed for scholars to be able to effortlessly proceed to bachelor degree courses. The 90 credit education meets the lower bachelor education requirements so when you earn your own associate of arts in BA online, you will get access to the modern technologies in this field. Several reasons why students would like to get their associate degree in business is because they may be interested in this area and want to get the general schooling necessary before jumping into a bachelor education program. Thanks alot : ) for the tips you actually provide in the blog.
Unquestionably believe that which you said. Your favorite reason seemed to be on the net the simplest thing to be aware of. I say to you, I definitely get annoyed while people consider worries that they plainly don’t know about. You managed to hit the nail upon the top and defined out the whole thing without having side effect , people could take a signal. Will probably be back to get more. Thanks
Valuable info. Lucky me I found your site by accident, and I am shocked why this accident did not happened earlier! I bookmarked it.
F*ckin? remarkable things here. I am very glad to see your post. Thanks so much and i’m looking ahead to contact you. Will you please drop me a e-mail?
I have realized some considerations through your site post. One other stuff I would like to say is that there are lots of games that you can buy which are designed mainly for toddler age young children. They include pattern identification, colors, dogs, and patterns. These normally focus on familiarization as an alternative to memorization. This will keep little ones engaged without having the experience like they are learning. Thanks
This website online is really a walk-through for the entire information you wished about this and didn?t know who to ask. Glimpse here, and also you?ll undoubtedly uncover it.
I have realized some significant things through your blog post post. One other point I would like to convey is that there are several games on the market designed specially for preschool age children. They consist of pattern acknowledgement, colors, wildlife, and shapes. These normally focus on familiarization instead of memorization. This will keep a child occupied without sensing like they are learning. Thanks
It is appropriate time to make some plans for the long run and it is time to be happy. I have learn this put up and if I could I wish to recommend you some fascinating things or suggestions. Perhaps you could write next articles relating to this article. I want to learn even more things about it!
I’m in awe of the author’s capability to make complicated concepts approachable to readers of all backgrounds. This article is a testament to her expertise and commitment to providing valuable insights. Thank you, author, for creating such an engaging and illuminating piece. It has been an unforgettable experience to read!
Hi there are using WordPress for your site platform? I’m new to the blog world but I’m trying to get started and create my own. Do you need any coding expertise to make your own blog? Any help would be greatly appreciated!
I just couldn’t depart your website prior to suggesting that I actually loved the usual information an individual supply in your guests? Is gonna be again frequently in order to check up on new posts
One thing I want to say is the fact that car insurance termination is a dreadful experience so if you’re doing the suitable things as being a driver you’ll not get one. A lot of people do are sent the notice that they are officially dumped by the insurance company and several have to struggle to get more insurance following a cancellation. Affordable auto insurance rates are frequently hard to get from a cancellation. Having the main reasons pertaining to auto insurance cancelling can help people prevent losing one of the most crucial privileges out there. Thanks for the suggestions shared via your blog.