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 } }
In line with my study, after a the foreclosure home is marketed at a bidding, it is common for that borrower to be able to still have a remaining balance on the bank loan. There are many loan merchants who seek to have all costs and liens paid back by the upcoming buyer. However, depending on certain programs, laws, and state laws there may be quite a few loans which aren’t easily sorted out through the exchange of lending options. Therefore, the responsibility still lies on the debtor that has got his or her property in foreclosure process. Thank you for sharing your thinking on this weblog.
The other day, while I was at work, my sister stole my iPad and tested to see if it can survive a 40 foot drop, just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views. I know this is totally off topic but I had to share it with someone!
Thanks for your tips. One thing I have noticed is that banks and also financial institutions know the dimensions and spending behavior of consumers and understand that the majority of people max out and about their real credit cards around the vacations. They sensibly take advantage of this kind of fact and start flooding your inbox and also snail-mail box using hundreds of no-interest APR card offers just after the holiday season comes to an end. Knowing that in case you are like 98 of the American general public, you’ll jump at the one opportunity to consolidate credit card debt and switch balances to 0 rate credit cards.
Generally I do not read post on blogs, but I wish to say that this write-up very forced me to try and do it! Your writing style has been amazed me. Thanks, quite nice post.
Some tips i have observed in terms of laptop or computer memory is the fact that there are features such as SDRAM, DDR etc, that must go with the specifications of the mother board. If the personal computer’s motherboard is reasonably current and there are no os issues, replacing the storage space literally requires under sixty minutes. It’s on the list of easiest laptop upgrade types of procedures one can consider. Thanks for spreading your ideas.
Thanks for the helpful article. It is also my belief that mesothelioma has an really long latency interval, which means that signs of the disease might not exactly emerge right until 30 to 50 years after the initial exposure to asbestos fiber. Pleural mesothelioma, which is the most common type and impacts the area throughout the lungs, might cause shortness of breath, chest muscles pains, plus a persistent cough, which may produce coughing up blood.
As I site possessor I believe the content material here is rattling excellent , appreciate it for your hard work. You should keep it up forever! Good Luck.
Current (Greek – ??????????? )
we can help you, among other things, prepare for it
To set up a business bank account in a digital bank, you should take the following steps:
If it is about organizing a lottery, installing slot machines or online casinos, you will need a Class 1 license.
Conclusion: Tax regulation for MSBs and FMSBs in Canada is a complex system that requires a detailed understanding and careful compliance with all tax rules and regulations. Proper tax planning and strategy can have a significant impact on the efficiency and profitability of MSB and FMSB operations in the Canadian market.
Accounting for changes in the structure and management of companies: Any changes in the management structure of the company or in its legal data must be registered in the Commercial Register, which ensures that the information is up-to-date and reliable.
Monitoring Changes in Legislation: Continuously monitor changes in AML/CFT laws and regulations and provide expert advice to senior management on the implications for compliance.
Requirements for Acquiring a Cryptocurrency License in Hong Kong
4. Perth
Assistance and compliance with NBA Technical Audit
I?ve recently started a website, the info you offer on this website has helped me greatly. Thanks for all of your time & work.
My brother suggested I might like this blog. He was totally right. This post actually made my day. You can not imagine just how much time I had spent for this info! Thanks!
Thanks a bunch for sharing this with all of us you really know what you’re speaking approximately! Bookmarked. Kindly also seek advice from my website =). We may have a link alternate contract between us!
Thank you for another informative blog. Where else could I get that type of information written in such an ideal way? I’ve a project that I am just now working on, and I have been on the look out for such information.
One more important aspect is that if you are a senior citizen, travel insurance with regard to pensioners is something you ought to really think about. The mature you are, the more at risk you might be for allowing something terrible happen to you while abroad. If you are not covered by a few comprehensive insurance policies, you could have a few serious issues. Thanks for giving your good tips on this website.
I’m really enjoying the design and layout of your blog. It’s a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire out a designer to create your theme? Great work!
Youre so cool! I dont suppose Ive read something like this before. So good to seek out any individual with some unique ideas on this subject. realy thank you for starting this up. this web site is something that’s wanted on the web, somebody with a little originality. helpful job for bringing something new to the internet!
It’s a pity you don’t have a donate button! I’d most certainly donate to this excellent blog! I suppose for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to new updates and will share this website with my Facebook group. Talk soon!
whoah this blog is magnificent i love reading your articles. Keep up the good work! You know, a lot of people are hunting around for this info, you could aid them greatly.
We are a gaggle of volunteers and opening a brand new scheme in our community. Your web site offered us with valuable information to paintings on. You’ve done an impressive task and our whole community will probably be grateful to you.
Once I initially commented I clicked the -Notify me when new feedback are added- checkbox and now each time a remark is added I get four emails with the same comment. Is there any manner you can remove me from that service? Thanks!
I am curious to find out what blog system you happen to be using? I’m having some minor security problems with my latest site and I’d like to find something more safeguarded. Do you have any suggestions?
I have realized that over the course of developing a relationship with real estate proprietors, you’ll be able to come to understand that, in most real estate deal, a payment is paid. All things considered, FSBO sellers really don’t “save” the fee. Rather, they struggle to earn the commission by doing the agent’s task. In the process, they expend their money as well as time to perform, as best they’re able to, the jobs of an realtor. Those duties include revealing the home by marketing, introducing the home to prospective buyers, building a sense of buyer emergency in order to make prompt an offer, organizing home inspections, managing qualification inspections with the lender, supervising fixes, and aiding the closing.
Hi! I’ve been following your site for some time now and finally got the courage to go ahead and give you a shout out from Austin Texas! Just wanted to mention keep up the excellent job!
It?s really a great and useful piece of information. I am satisfied that you shared this helpful information with us. Please keep us informed like this. Thanks for sharing.
I’ve been browsing online more than 3 hours today, yet I never found any interesting article like yours. It?s pretty worth enough for me. In my view, if all web owners and bloggers made good content as you did, the net will be a lot more useful than ever before.
Wow, wonderful weblog format! How lengthy have you been blogging for? you made running a blog glance easy. The whole glance of your web site is fantastic, as neatly as the content material!
Hello! I’ve been reading your blog for some time now and finally got the bravery to go ahead and give you a shout out from Kingwood Texas! Just wanted to mention keep up the great work!
Thanks a bunch for sharing this with all of us you actually know what you’re talking about! Bookmarked. Kindly also visit my site =). We could have a link exchange agreement between us!
лечение наркозависимости в стационаре лечение наркозависимости в стационаре .
certainly like your web-site but you have to check the spelling on quite a few of your posts. Several of them are rife with spelling issues and I find it very troublesome to tell the truth nevertheless I will surely come back again.
Good day! This is kind of off topic but I need some guidance from an established blog. Is it very 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