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 absolutely love your blog and find the majority of your post’s to be exactly I’m looking for. can you offer guest writers to write content for yourself? I wouldn’t mind composing a post or elaborating on a few of the subjects you write concerning here. Again, awesome web log!
Great write-up, I?m normal visitor of one?s web site, maintain up the nice operate, and It is going to be a regular visitor for a long time.
Take a look at my page :: Find top-rated certified Daycares in your area
I?m impressed, I must say. Really rarely do I encounter a blog that?s both educative and entertaining, and let me let you know, you have hit the nail on the head. Your idea is excellent; the difficulty is something that not enough people are talking intelligently about. I’m very happy that I stumbled throughout this in my seek for one thing referring to this.
Hello there, just became aware of your blog through Google, and found that it is truly informative. I?m going to watch out for brussels. I?ll appreciate if you continue this in future. Numerous people will be benefited from your writing. Cheers!
I’ve been surfing online greater than three hours lately, but I never found any attention-grabbing article like yours. It?s pretty worth sufficient for me. In my opinion, if all web owners and bloggers made good content as you did, the internet can be a lot more helpful than ever before.
obviously like your web-site but you need to check the spelling on quite a few of your posts. Many of them are rife with spelling problems and I find it very bothersome to tell the truth nevertheless I?ll surely come back again.
my webpage – Daycare Near Me By State
What an eye-opening and meticulously-researched article! The author’s meticulousness and aptitude to present complicated ideas in a understandable manner is truly commendable. I’m thoroughly enthralled by the depth of knowledge showcased in this piece. Thank you, author, for sharing your expertise with us. This article has been a true revelation!
Thanks for your posting. What I want to comment on is that while looking for a good on the net electronics retail outlet, look for a website with total information on important factors such as the personal privacy statement, basic safety details, payment procedures, as well as other terms along with policies. Usually take time to look into the help along with FAQ segments to get a far better idea of how a shop operates, what they are capable of doing for you, and the way you can take full advantage of the features.
Thanks a lot for the helpful post. It is also my belief that mesothelioma has an really long latency time, which means that indication of the disease might not exactly emerge right up until 30 to 50 years after the primary exposure to asbestos fiber. Pleural mesothelioma, and that is the most common type and impacts the area round the lungs, could potentially cause shortness of breath, upper body pains, including a persistent cough, which may lead to coughing up blood.
I have realized that over the course of making a relationship with real estate proprietors, you’ll be able to get them to understand that, in each and every real estate financial transaction, a commission rate is paid. All things considered, FSBO sellers will not “save” the commission payment. Rather, they fight to earn the commission simply by doing a great agent’s task. In doing so, they invest their money and time to perform, as best they might, the assignments of an adviser. Those duties include getting known the home by means of marketing, representing the home to willing buyers, making a sense of buyer desperation in order to trigger an offer, making arrangement for home inspections, dealing with qualification inspections with the loan company, supervising fixes, and facilitating the closing.
You can definitely see your expertise in the work you write. The world hopes for more passionate writers like you who are not afraid to say how they believe. Always follow your heart.
I was very happy to find this web-site.I wanted to thanks to your time for this glorious learn!! I undoubtedly having fun with every little little bit of it and I’ve you bookmarked to take a look at new stuff you weblog post.
Hi my friend! I wish to say that this article is awesome, nice written and include approximately all important infos. I would like to see more posts like this.
Somebody essentially help to make seriously posts I would state. This is the first time I frequented your web page and thus far? I amazed with the research you made to create this particular publish amazing. Fantastic job!
I have recently started a website, the info you provide on this web site has helped me tremendously. Thanks for all of your time & work.
I’ve been absent for a while, but now I remember why I used to love this blog. Thank you, I?ll try and check back more often. How frequently you update your site?
Heya i?m for the first time here. I came across this board and I find It truly useful & it helped me out much. I hope to give something back and help others like you aided me.
This is very interesting, You’re an excessively skilled blogger. I’ve joined your feed and sit up for in search of extra of your wonderful post. Also, I’ve shared your web site in my social networks!
Youre so cool! I dont suppose Ive learn anything like this before. So good to search out any individual with some unique thoughts on this subject. realy thank you for beginning this up. this web site is one thing that’s needed on the internet, someone with slightly originality. useful job for bringing one thing new to the internet!
вызвать капельнцу от запоя http://snyatie-zapoya-na-domu11.ru/ .
Thank you, I’ve just been searching for info about this subject for ages and yours is the greatest I’ve discovered till now. But, what about the bottom line? Are you sure about the source?
This actually answered my drawback, thanks!
There may be noticeably a bundle to know about this. I assume you made sure nice factors in features also.
This is very fascinating, You are an overly professional blogger. I’ve joined your feed and sit up for looking for extra of your fantastic post. Additionally, I have shared your website in my social networks!
Once I originally commented I clicked the -Notify me when new feedback are added- checkbox and now every time a comment is added I get 4 emails with the same comment. Is there any means you can take away me from that service? Thanks!
I have realized that over the course of making a relationship with real estate proprietors, you’ll be able to come to understand that, in every real estate purchase, a commission rate is paid. In the long run, FSBO sellers tend not to “save” the commission payment. Rather, they struggle to win the commission by doing a strong agent’s work. In the process, they devote their money and also time to accomplish, as best they’re able to, the tasks of an adviser. Those jobs include exposing the home through marketing, showing the home to willing buyers, making a sense of buyer urgency in order to prompt an offer, preparing home inspections, handling qualification investigations with the loan company, supervising maintenance tasks, and facilitating the closing.
I appreciate, cause I found exactly what I was looking for. You’ve ended my 4 day long hunt! God Bless you man. Have a nice day. Bye
Thanks for every other magnificent article. Where else could anyone get that kind of information in such an ideal means of writing? I’ve a presentation subsequent week, and I am on the look for such info.