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 } }
Howdy! I’m at work surfing around your blog from my new apple iphone! Just wanted to say I love reading your blog and look forward to all your posts! Keep up the excellent work!
I’m not sure why but this weblog is loading extremely slow for me. Is anyone else having this issue or is it a problem on my end? I’ll check back later on and see if the problem still exists.
One more thing. I believe that there are lots of travel insurance web-sites of respected companies that allow you enter a trip details and get you the insurance quotes. You can also purchase an international travel insurance policy on internet by using your own credit card. All you need to do would be to enter all your travel particulars and you can view the plans side-by-side. Just find the plan that suits your finances and needs then use your bank credit card to buy the idea. Travel insurance on the web is a good way to check for a dependable company with regard to international travel cover. Thanks for revealing your ideas.
Thanks a lot for sharing this with all of us you really know what you’re talking about! Bookmarked. Please also visit my web site =). We could have a link exchange agreement between us!
I have really learned newer and more effective things as a result of your web site. One other thing I’d really like to say is that newer personal computer operating systems often allow extra memory to get used, but they in addition demand more memory space simply to operate. If a person’s computer can’t handle a lot more memory as well as newest software program requires that ram increase, it may be the time to buy a new PC. Thanks
canadian pharmacy ltd: canada pharmacy 24h – usa canada pharm
Heeft iemand ervaring met Qbet Casino bonussen?
Hallo allemaal, ik heb net het welkomstpakket van Qbet Casino geprobeerd (300% + 150 gratis spins). Na het behalen van de speelvoorwaarden (slechts 35x) kreeg ik mijn uitbetaling binnen 4 uur op Skrill!
Wie heeft er nog meer ervaring met Qbet? Zijn hun live casino dealers echt zo goed als ze zeggen?
Website: https://qbet-casino1.com/
P.S. Hun wekelijkse 10% cashback op sportverliezen is ook echt de moeite waard!”
Thanks for the ideas you have shared here. On top of that, I believe there are several factors which really keep your auto insurance premium all the way down. One is, to contemplate buying cars and trucks that are from the good list of car insurance businesses. Cars that happen to be expensive tend to be at risk of being robbed. Aside from that insurance coverage is also in accordance with the value of the car, so the higher priced it is, then the higher the actual premium you spend.
This is the suitable blog for anybody who desires to find out about this topic. You realize a lot its almost arduous to argue with you (not that I truly would want?HaHa). You positively put a new spin on a subject thats been written about for years. Nice stuff, simply nice!
great points altogether, you just gained a new reader. What would you recommend about your publish that you made a few days ago? Any certain?
Hello! This is kind of off topic but I need some guidance from an established blog. Is it hard to set up your own blog? I’m not very techincal but I can figure things out pretty fast. I’m thinking about making my own but I’m not sure where to start. Do you have any ideas or suggestions? Appreciate it
Feel free to surf to my website :: PokerTube – Watch Free Poker Videos & TV Shows
Hi, Neat post. There’s an issue along with your web site in internet explorer, could check this? IE nonetheless is the market leader and a big element of other folks will leave out your magnificent writing due to this problem.
usa canada pharm: usa canada pharm – USACanadaPharm
Currently it sounds like Expression Engine is the best blogging platform out there right now. (from what I’ve read) Is that what you’re using on your blog?
Great work! This is the type of information that should be shared around the web. Shame on Google for not positioning this post higher! Come on over and visit my web site . Thanks =)
Thanks for your post. One other thing is that if you are marketing your property on your own, one of the challenges you need to be conscious of upfront is just how to deal with property inspection records. As a FSBO owner, the key to successfully moving your property along with saving money upon real estate agent commission rates is understanding. The more you know, the more stable your sales effort will be. One area that this is particularly crucial is information about home inspections.
With havin so much content do you ever run into any issues of plagorism or copyright violation? My website has a lot of exclusive content I’ve either written myself or outsourced but it appears a lot of it is popping it up all over the internet without my authorization. Do you know any techniques to help reduce content from being stolen? I’d truly appreciate it.
Thanks for the points you have discussed here. Something important I would like to talk about is that computer memory demands generally go up along with other developments in the know-how. For instance, whenever new generations of processor chips are brought to the market, there’s usually a matching increase in the size calls for of both laptop or computer memory plus hard drive room. This is because the software operated by means of these processor chips will inevitably rise in power to use the new technological innovation.
Howdy! I know this is kinda off topic nevertheless I’d figured I’d ask. Would you be interested in trading links or maybe guest writing a blog post or vice-versa? My blog covers a lot of the same subjects as yours and I believe we could greatly benefit from each other. If you might be interested feel free to send me an email. I look forward to hearing from you! Excellent blog by the way!
canadadrugpharmacy com onlinecanadianpharmacy best canadian online pharmacy
Hi there! This is my first visit to your blog! We are a group of volunteers and starting a new initiative in a community in the same niche. Your blog provided us beneficial information to work on. You have done a extraordinary job!
What?s Happening i’m new to this, I stumbled upon this I’ve found It positively useful and it has aided me out loads. I hope to contribute & help other users like its aided me. Good job.
usa canada pharm: canadian drugs pharmacy – recommended canadian pharmacies
https://usacanadapharm.com/# USACanadaPharm
canadian online pharmacy: usa canada pharm – legitimate canadian pharmacy
usa canada pharm: USACanadaPharm – USACanadaPharm
canadian pharmacy scam usa canada pharm reliable canadian pharmacy
https://usacanadapharm.com/# canadian pharmacy victoza
canadian family pharmacy: USACanadaPharm – USACanadaPharm