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 } }
what happens when you mix cialis with grapefruit?: cialis prescription assistance program – buy cialis with american express
https://tadalaccess.com/# cialis store in philippines
https://tadalaccess.com/# side effects of cialis
mint pharmaceuticals tadalafil reviews: Tadal Access – tadalafil cheapest online
I mastered more new things on this fat reduction issue. Just one issue is a good nutrition is extremely vital while dieting. A huge reduction in bad foods, sugary foods, fried foods, sugary foods, red meat, and whitened flour products could possibly be necessary. Holding wastes bloodsuckers, and toxins may prevent ambitions for shedding fat. While particular drugs momentarily solve the challenge, the nasty side effects usually are not worth it, and in addition they never offer you more than a short-lived solution. It can be a known undeniable fact that 95 of dietary fads fail. Many thanks sharing your opinions on this site.
Excellent read, I just passed this onto a friend who was doing some research on that. And he actually bought me lunch as I found it for him smile So let me rephrase that: Thanks for lunch!
best place to buy tadalafil online: Tadal Access – san antonio cialis doctor
buy facebook ad account https://buy-ad-accounts.click
buying facebook account cheap facebook accounts
facebook ads accounts fb accounts for sale
what does cialis look like cialis for women whats cialis
https://tadalaccess.com/# cialis insurance coverage blue cross
cialis dapoxetine europe tadalafil hong kong dapoxetine and tadalafil
cialis side effects: over the counter drug that works like cialis – does medicare cover cialis
I don?t even know how I ended up here, but I thought this post used to be great. I do not know who you might be but definitely you are going to a well-known blogger if you happen to are not already 😉 Cheers!
I have not checked in here for a while because I thought it was getting boring, but the last several posts are great quality so I guess I?ll add you back to my daily bloglist. You deserve it my friend 🙂
It’s a pity you don’t have a donate button! I’d without a doubt donate to this excellent 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. Chat soon!
Thanks for your write-up. I have constantly seen that most people are desperate to lose weight because they wish to look slim in addition to looking attractive. Having said that, they do not usually realize that there are many benefits just for losing weight in addition. Doctors insist that obese people experience a variety of disorders that can be perfectely attributed to their particular excess weight. The good thing is that people who’re overweight and also suffering from several diseases can reduce the severity of the illnesses by simply losing weight. You possibly can see a progressive but notable improvement in health whenever even a moderate amount of losing weight is obtained.
You could definitely see your skills in the work you write. The world hopes for even more passionate writers like you who aren’t afraid to say how they believe. Always go after your heart.
great points altogether, you just gained a new reader. What would you suggest in regards to your post that you made some days ago? Any positive?
A lot of what you mention is supprisingly accurate and that makes me wonder why I had not looked at this with this light before. This particular piece really did switch the light on for me as far as this particular subject matter goes. Nevertheless there is one particular issue I am not really too comfortable with so whilst I try to reconcile that with the core idea of the position, let me see exactly what the rest of the subscribers have to point out.Very well done.
cialis for sale toronto: sunrise remedies tadalafil – what is cialis prescribed for
tadalafil brand name: Tadal Access – cialis no perscription overnight delivery
Hello There. I found your blog using msn. This is a really well written article. I will be sure to bookmark it and come back to read more of your useful info. Thanks for the post. I?ll definitely return.
very good submit, i certainly love this website, carry on it
generic cialis available in canada: TadalAccess – where can i buy cialis online
Please let me know if you’re looking for a author for your site. You have some really good posts and I feel I would be a good asset. If you ever want to take some of the load off, I’d love to write some articles for your blog in exchange for a link back to mine. Please send me an e-mail if interested. Many thanks!
does tadalafil lower blood pressure: TadalAccess – maximum dose of cialis in 24 hours
Thanks for the helpful content. It is also my belief that mesothelioma cancer has an incredibly long latency time, which means that warning signs of the disease might not emerge until finally 30 to 50 years after the first exposure to mesothelioma. Pleural mesothelioma, and that is the most common sort and is affecting the area round the lungs, may cause shortness of breath, torso pains, and also a persistent cough, which may produce coughing up blood vessels.
Great website. Plenty of useful info here. I am sending it to several friends ans also sharing in delicious. And of course, thanks for your effort!