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’ve been surfing online greater than three hours today, but I by no means discovered any interesting article like yours. It is pretty worth enough for me. In my view, if all website owners and bloggers made good content as you did, the web shall be much more helpful than ever before.
Thanks for the distinct tips provided on this weblog. I have realized that many insurers offer consumers generous deals if they opt to insure multiple cars with them. A significant quantity of households have got several cars or trucks these days, specially those with mature teenage children still residing at home, as well as savings for policies could soon increase. So it will pay to look for a great deal.
This is really interesting, You’re a very skilled blogger. I have joined your rss feed and look forward to seeking more of your fantastic post. Also, I have shared your website in my social networks!
Does your website have a contact page? I’m having problems locating it but, I’d like to shoot you an e-mail. I’ve got some suggestions for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it improve over time.
F*ckin? remarkable issues here. I am very glad to look your post. Thanks so much and i’m having a look forward to contact you. Will you kindly drop me a e-mail?
My brother recommended I may like this web site. He used to be entirely right. This submit truly made my day. You can not believe simply how a lot time I had spent for this info! Thanks!
Thank you a bunch for sharing this with all of us you really realize what you’re speaking approximately! Bookmarked. Kindly additionally talk over with my web site =). We could have a hyperlink exchange arrangement among us!
sams club pharmacy levitra: v pharmacy accutane – pharmacy website
Use the promo code when registering on 1win and get a 100% bonus on your first deposit.
I do not even know how I ended up here, but I thought this post was good. I don’t know who you are but certainly you are going to a famous blogger if you aren’t already 😉 Cheers!
I think that is one of the most important information for me. And i’m glad reading your article. However wanna commentary on some basic things, The website style is perfect, the articles is in reality nice : D. Just right activity, cheers
I have realized some important matters through your blog post. One other stuff I would like to convey is that there are lots of games available and which are designed specially for preschool age small children. They include things like pattern recognition, colors, animals, and shapes. These normally focus on familiarization rather than memorization. This makes children engaged without having the experience like they are learning. Thanks
Hi are using WordPress for your blog platform? I’m new to the blog world but I’m trying to get started and create my own. Do you need any coding knowledge to make your own blog? Any help would be really appreciated!
Heya just wanted to give you a brief heads up and let you know a few of the images aren’t loading correctly. I’m not sure why but I think its a linking issue. I’ve tried it in two different internet browsers and both show the same results.
Normally I don’t learn post on blogs, but I would like to say that this write-up very pressured me to take a look at and do so! Your writing taste has been amazed me. Thanks, quite great article.
We’re a bunch of volunteers and opening a brand new scheme in our community. Your site offered us with useful information to work on. You’ve performed a formidable activity and our whole neighborhood might be grateful to you.
cialis pharmacy coupon: reddit best online pharmacy – Olanzapine
Prevacid: target pharmacy online refills – the drug store pharmacy
online pharmacy kamagra jelly: online pharmacies that use paypal – blood pressure
Feel free to surf to my page :: Daycare Near Me
наркологическая скорая помощь москва наркологическая скорая помощь москва .
снятие ломки недорого снятие ломки недорого .
I just couldn’t depart your web site prior to suggesting that I actually enjoyed the standard info a person provide for your visitors? Is going to be back often to check up on new posts
https://easydrugrx.com/# wegmans online pharmacy
provigil pharmacy online
Hello! I know this is kinda off topic but I was wondering if you knew where I could find a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having problems finding one? Thanks a lot!
It’s a shame you don’t have a donate button! I’d definitely donate to this outstanding blog! I suppose for now i’ll settle for bookmarking and adding your RSS feed to my Google account. I look forward to fresh updates and will share this site with my Facebook group. Talk soon!
Thanks for this wonderful article. Also a thing is that the majority of digital cameras come equipped with the zoom lens so that more or less of a scene being included by way of ‘zooming’ in and out. Most of these changes in {focus|focusing|concentration|target|the a**** length are reflected from the viewfinder and on massive display screen on the back of the exact camera.
Today, with the fast way of life that everyone leads, credit cards get this amazing demand throughout the market. Persons from every discipline are using credit card and people who aren’t using the credit card have arranged to apply for even one. Thanks for sharing your ideas on credit cards.
I used to be recommended this website via my cousin. I’m not sure whether or not this post is written through him as nobody else recognize such distinctive about my difficulty. You’re wonderful! Thank you!
Very nice post. I just stumbled upon your blog and wished to say that I’ve truly loved surfing around your blog posts. In any case I will be subscribing to your feed and I am hoping you write once more very soon!