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 } }
Thanks for ones marvelous posting! I definitely enjoyed reading it, you happen to be a great author.I will be sure to bookmark your blog and definitely will come back later in life. I want to encourage you to continue your great posts, have a nice weekend!
Guvenilir bir kumarhane tavsiye ederim Sweet bonanza
En iyi kumarhanede buyuk kazan?n Sweet bonanza
Hello! Do you know if they make any plugins to safeguard against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any recommendations?
I have learned some points through your blog post post. One other subject I would like to say is that there are numerous games available on the market designed specifically for preschool age youngsters. They incorporate pattern acceptance, colors, pets, and models. These typically focus on familiarization as opposed to memorization. This keeps little kids occupied without feeling like they are learning. Thanks
Hey very cool website!! Man .. Excellent .. Amazing .. I’ll bookmark your blog and take the feeds also?I am happy to find numerous useful information here in the post, we need develop more techniques in this regard, thanks for sharing. . . . . .
I just could not go away your website prior to suggesting that I actually enjoyed the usual info an individual supply in your guests? Is going to be back steadily to inspect new posts
Thanks for your submission. I also believe laptop computers are becoming more and more popular right now, and now are sometimes the only form of computer employed in a household. The reason being at the same time that they are becoming more and more economical, their computing power is growing to the point where there’re as strong as desktop from just a few years ago.
Out of my observation, shopping for electronic devices online may be easily expensive, however there are some how-to’s that you can use to acquire the best discounts. There are often ways to discover discount discounts that could help make one to hold the best electronic products products at the cheapest prices. Good blog post.
It’s a pity you don’t have a donate button! I’d definitely donate to this brilliant blog! I guess for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to fresh updates and will share this blog with my Facebook group. Talk soon!
Hi colleagues, how is all, and what you desire to say about this article, in my view its in fact
amazing in favor of me.
My brother recommended I would possibly like this web site. He used to be totally right. This post actually made my day. You cann’t imagine just how much time I had spent for this info! Thanks!
Thanks for your write-up. One other thing is individual states in the United states of america have their very own laws that affect home owners, which makes it quite hard for the our elected representatives to come up with a whole new set of rules concerning property foreclosure on householders. The problem is that a state possesses own legal guidelines which may have interaction in an adverse manner on the subject of foreclosure guidelines.
WONDERFUL Post.thanks for share..extra wait .. ?
Hello, you used to write fantastic, but the last several posts have been kinda boring? I miss your great writings. Past several posts are just a little bit out of track! come on!
Herkesin tavsiye ettigi kumarhane Sweet bonanza
Sans?n?z guvenilir bir kumarhanede Sweet bonanza
Pretty part of content. I simply stumbled upon your weblog and in accession capital to say that I acquire actually enjoyed account your blog posts. Any way I?ll be subscribing in your augment or even I achievement you access constantly rapidly.
Hi, i believe that i saw you visited my blog so i got here to ?go back the prefer?.I am attempting to find things to improve my site!I assume its ok to make use of some of your ideas!!
whoah this weblog is fantastic i really like studying your posts. Stay up the good paintings! You know, lots of individuals are looking around for this information, you can aid them greatly.
Thanks for revealing your ideas on this blog. As well, a fable regarding the banking institutions intentions when talking about home foreclosure is that the loan company will not take my repayments. There is a fair bit of time in which the bank will take payments every now and then. If you are as well deep in the hole, they’ll commonly demand that you pay the actual payment entirely. However, i am not saying that they will not take any sort of installments at all. In the event you and the standard bank can have the ability to work some thing out, the foreclosure course of action may stop. However, should you continue to miss payments beneath new program, the foreclosed process can just pick up where it left off.
I have observed that over the course of developing a relationship with real estate owners, you’ll be able to come to understand that, in every single real estate exchange, a commission rate is paid. Eventually, FSBO sellers tend not to “save” the fee. Rather, they struggle to win the commission by doing a good agent’s occupation. In doing this, they expend their money and time to conduct, as best they might, the responsibilities of an realtor. Those tasks include disclosing the home by means of marketing, showing the home to buyers, making a sense of buyer urgency in order to trigger an offer, making arrangement for home inspections, taking on qualification check ups with the lender, supervising repairs, and aiding the closing.
Bu kumarhanedeki en iyi oyunlar Sweet bonanza
Hello, i think that i noticed you visited my blog so i got here to ?return the desire?.I am trying to to find issues to improve my site!I guess its adequate to make use of some of your concepts!!
Thanks for the tips you have contributed here. Also, I believe there are numerous factors that keep your car insurance premium decrease. One is, to take into account buying motors that are from the good directory of car insurance businesses. Cars which can be expensive tend to be more at risk of being stolen. Aside from that insurance coverage is also in accordance with the value of your car, so the more expensive it is, then the higher the premium you have to pay.
En guvenilir kumarhanede kazan?n Sweet bonanza
I’m really inspired with your writing skills and also with the format on your blog. Is that this a paid subject or did you modify it yourself? Either way stay up the excellent high quality writing, it?s rare to look a nice blog like this one nowadays..
It’s a shame you don’t have a donate button! I’d most certainly donate to this brilliant blog! I guess for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to brand new updates and will talk about this site with my Facebook group. Talk soon!
Hiya very cool blog!! Guy .. Excellent .. Wonderful .. I’ll bookmark your website and take the feeds also?I am satisfied to search out numerous useful info here in the submit, we need work out more strategies in this regard, thanks for sharing. . . . . .
Thank you for every other informative website. Where else may just I am getting that kind of information written in such a perfect manner? I have a mission that I’m simply now working on, and I have been on the look out for such info.