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 } }
This is in contrast to gold or commodity futures contracts, which specify a price for the commodity for future delivery date.
Thanks for your article. One other thing is that if you are selling your property all on your own, one of the challenges you need to be alert to upfront is how to deal with property inspection accounts. As a FSBO supplier, the key concerning successfully switching your property and also saving money upon real estate agent profits is information. The more you are aware of, the simpler your home sales effort are going to be. One area when this is particularly significant is information about home inspections.
The man turning jet planes into cool houses
анальный секс смотреть
Wasilla, south central Alaska. Home to bears, lakes, mountains and a flight school that’s fast becoming a private aviation wonderland.
At FLY8MA Pilot Lodge, you can opt for a scenic flight tour with glacier views, take the controls for a flying lesson, or go all in and get your pilot training.
When night falls over the broad vistas of the US state they call the Last Frontier, you can then climb the steps to two unique accommodation experiences: a converted McDonnell Douglas DC-6 airplane and the newest arrival, a McDonnell Douglas DC-9 – still with its DHL livery.
The fast-developing site is an ongoing project by FLY8MA founder Jon Kotwicki, who previously owned a flight school in Florida, before working as a commercial pilot and eventually ending up in Alaska.
Flying for the airlines “pays good money and everything, but it’s a very boring job,” he says. “Driving Uber is more interesting because you could talk to your passengers.”
Having fallen in love with the south central region on a vacation spent hiking, fishing and spotting bears and grizzlies, he chose it as a spot where he and his team – and his trusty Pomeranian dog Foxtrot – could “buy a lot of property and perhaps develop our own airport and run our own show.”
Wonderful work! This is the type of information that should be shared around the net. Shame on Google for not positioning this post higher! Come on over and visit my site . Thanks =)
For example, they may be made recycled materials but include lots of unnecessary packaging or use a large amount of energy.
Hey would you mind stating which blog platform you’re working with? I’m looking to start my own blog in the near future but I’m having a hard time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I’m looking for something completely unique. P.S My apologies for being off-topic but I had to ask!
reputable indian pharmacies: india pharmacy mail order – top 10 online pharmacy in india
The man turning jet planes into cool houses
анальный секс зрелых
Wasilla, south central Alaska. Home to bears, lakes, mountains and a flight school that’s fast becoming a private aviation wonderland.
At FLY8MA Pilot Lodge, you can opt for a scenic flight tour with glacier views, take the controls for a flying lesson, or go all in and get your pilot training.
When night falls over the broad vistas of the US state they call the Last Frontier, you can then climb the steps to two unique accommodation experiences: a converted McDonnell Douglas DC-6 airplane and the newest arrival, a McDonnell Douglas DC-9 – still with its DHL livery.
The fast-developing site is an ongoing project by FLY8MA founder Jon Kotwicki, who previously owned a flight school in Florida, before working as a commercial pilot and eventually ending up in Alaska.
Flying for the airlines “pays good money and everything, but it’s a very boring job,” he says. “Driving Uber is more interesting because you could talk to your passengers.”
Having fallen in love with the south central region on a vacation spent hiking, fishing and spotting bears and grizzlies, he chose it as a spot where he and his team – and his trusty Pomeranian dog Foxtrot – could “buy a lot of property and perhaps develop our own airport and run our own show.”
Most of us have probably used the “Amazon” app for shopping, right?
mexican drugstore online: mexican mail order pharmacies – best online pharmacies in mexico
It relocated its head workplaces from the previous constructing to nearby areas, including the Insular Life Building, Ayala North Exchange, Makati Stock Trade constructing, and BPI Buendia Heart in Makati, while other places of work moved to Vertis North in Quezon City and in Alabang, Muntinlupa.
Crawford, Krysten. “Martha likely to do prison time.” CNN Money.
Standard fashions for this are based on the premise that the chance of excessive variations of asset prices could be very low.
Thanks for the concepts you are sharing on this website. Another thing I would like to say is the fact that getting hold of duplicates of your credit file in order to look at accuracy of the detail could be the first motion you have to perform in repairing credit. You are looking to freshen your credit history from dangerous details problems that ruin your credit score.
This means financial institutions have to be clear on what their liquidity position on a daily basis.
Lang, Brent; Donnelly, Matt (August 1, 2019).
Thanks for another great article. Where else may just anyone get that kind of info in such an ideal approach of writing? I’ve a presentation next week, and I am on the look for such information.
Wonderful blog! I found it while surfing around on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Cheers
you’re really a good webmaster. The web site loading speed is incredible. It seems that you are doing any unique trick. Moreover, The contents are masterwork. you have done a great job on this topic!
Wow! I’m in awe of the author’s writing skills and ability to convey intricate concepts in a concise and concise manner. This article is a true gem that deserves all the praise it can get. Thank you so much, author, for offering your expertise and giving us with such a priceless treasure. I’m truly thankful!
https://easyrxcanada.com/# canadian medications
An investment banker, first, carefully analyses the portfolio of the person or the business.
There is lower liquidity for the futures contract which has an expiration date which is quite distant.
The conventional 2009 Ford Escape gets 20 miles per gallon (8.5 kilometers per liter) in the city and 28 miles per gallon (11.9 kilometers per liter) on the highway, according to the Environmental Protection Agency (EPA).
Most Penny Stocks are traded at a price lower than $5.00 per share.
https://easyrxcanada.com/# canadian pharmacy com
Because of this, unless you are a professional investor yourself, you almost assuredly could benefit by utilizing a private banking service of some kind.
Excellent post. I used to be checking constantly this weblog and I am impressed! Very useful info specially the closing part 🙂 I care for such information a lot. I used to be seeking this particular information for a long time. Thank you and good luck.
My brother suggested I might like this website. He was once entirely right. This put up actually made my day. You can not consider just how much time I had spent for this information! Thank you!
Standardization below EU Competition Guidelines and US Antitrust Legal guidelines: The Rise and Limits of Self-Regulation.