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 } }
https://interpharmonline.shop/# ed meds online canada
best canadian pharmacy online
canadian pharmacy uk delivery: highest rated canadian online pharmacy – canadian family pharmacy
1 вин. 1win6012.ru .
1win online http://1win814.ru .
партнёрка 1win https://boardwars.forum24.ru/?1-10-0-00000406-000-0-0/ .
HHN 25 featured 9 haunted houses, with admission reaching $101.99 through the prime days.
MexicanPharmInter mexican drug stores online buying from online mexican pharmacy
hello!,I like your writing so much! share we communicate more about your article on AOL? I require an expert on this area to solve my problem. Maybe that’s you! Looking forward to see you.
One thing I want to reply to is that weight loss program fast can be performed by the right diet and exercise. People’s size not just affects the look, but also the general quality of life. Self-esteem, depressive disorder, health risks, and also physical ability are impacted in excess weight. It is possible to do everything right whilst still having a gain. Should this happen, a medical problem may be the reason. While an excessive amount of food and not enough physical exercise are usually responsible, common medical conditions and widely used prescriptions may greatly add to size. Thanks for your post here.
Awesome article post.Thanks Again. Much obliged.
Great site. Plenty of useful information here. I am sending it to some friends ans also sharing in delicious. And certainly, thanks for your effort!
https://mexicanpharminter.com/# mexican drug stores online
You made some respectable factors there. I regarded on the web for the issue and found most people will go together with along with your website.
Hi just wanted to give you a quick heads up and let you know a few of the images aren’t loading properly. 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.
With havin so much content do you ever run into any problems of plagorism or copyright violation? My site has a lot of unique content I’ve either written myself or outsourced but it appears a lot of it is popping it up all over the web without my authorization. Do you know any techniques to help reduce content from being ripped off? I’d certainly appreciate it.
Hello! Do you use Twitter? I’d like to follow you if that would be ok. I’m absolutely enjoying your blog and look forward to new posts.
A person essentially help to make seriously posts I would state. This is the very first time I frequented your web page and thus far? I surprised with the research you made to create this particular publish incredible. Excellent job!
Good ? I should certainly pronounce, impressed with your site. I had no trouble navigating through all tabs as well as related info ended up being truly easy to do to access. I recently found what I hoped for before you know it in the least. Reasonably unusual. Is likely to appreciate it for those who add forums or anything, web site theme . a tones way for your client to communicate. Nice task..
Hi are using WordPress for your blog platform? I’m new to the blog world but I’m trying to get started and set up my own. Do you need any coding knowledge to make your own blog? Any help would be really appreciated!
https://interpharmonline.shop/# canadian drugs pharmacy
buying prescription drugs from india: IndiaMedFast.com – lowest prescription prices online india
Hey very cool blog!! Man .. Beautiful .. Amazing .. I’ll bookmark your web site and take the feeds also?I’m happy to find so many useful info here in the post, we need work out more techniques in this regard, thanks for sharing. . . . . .
Interesting blog post. What I would like to bring about is that pc memory ought to be purchased if the computer is unable to cope with everything you do along with it. One can put in two RAM memory boards containing 1GB each, as an illustration, but not certainly one of 1GB and one with 2GB. One should check the maker’s documentation for own PC to make certain what type of ram is necessary.
cheapest online pharmacy india order medicines online india cheapest online pharmacy india
Hi, i feel that i noticed you visited my weblog so i got here to ?return the want?.I am trying to find issues to improve my web site!I assume its ok to use some of your ideas!!
Elara Finance is transforming decentralized lending by offering secure, transparent, and flexible crypto loan solutions. Built on blockchain technology, Elara Finance enables users to borrow and lend digital assets seamlessly without intermediaries. With low-interest rates, automated smart contracts, and a permissionless DeFi environment, Elara Finance is making crypto lending accessible and profitable for investors worldwide. https://elara.ink
india online pharmacy store: IndiaMedFast – order medicines online india
Hi there, just became alert to your blog through Google, and found that it’s truly informative. I am gonna watch out for brussels. I?ll be grateful if you continue this in future. A lot of people will be benefited from your writing. Cheers!
mexican pharmacy online store: mexican drug stores online – Mexican Pharm Inter
Another issue is that video games can be serious as the name indicated with the principal focus on finding out rather than enjoyment. Although, it has an entertainment facet to keep your young ones engaged, every single game is often designed to work on a specific group of skills or course, such as mathmatical or technology. Thanks for your post.