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 } }
The covariant derivative of a covector subject alongside a vector field v is again a covector area.
Thanks for your write-up. One other thing is that if you are advertising your property by yourself, one of the problems you need to be aware about upfront is when to deal with house inspection accounts. As a FSBO supplier, the key towards successfully shifting your property and saving money on real estate agent revenue is information. The more you realize, the simpler your home sales effort are going to be. One area where this is particularly critical is reports.
buy zithromax online australia ZithroPharm buy cheap zithromax online
875 mg amoxicillin cost: amoxicillin 500 mg where to buy – amoxicillin 500 mg for sale
Register with the unique 1xBet promo code “1XBUM” to take advantage of a 100% deposit match welcome offer, reaching up to €/$130. In this guide, I will walk you through the top 1xBet promotions, which feature the sportsbook deposit bonus and the casino Welcome Package for 2025. In the current landscape of numerous sports betting platforms, we consider 1xBet to be among the top choices available, and for valid reasons. The range of products offered is impressive, the site’s security measures are outstanding, and those who register using the 1xBet promo code will find additional benefits.
how to get a promo code for 1xbet
canadian online pharmacies legitimate by aarp
legit canadian pharmacy
where can i buy zithromax medicine: Zithro Pharm – buy zithromax online
The promo code for 1XBET is “1XBUM.” By utilizing this code, you can unlock a special bonus for sports betting, offering a 100% match up to $130, or for casino games, a bonus of $1,950 along with 150 free spins. New players have the opportunity to input our 1xBet bonus code to enhance the standard sports free bet bonus by an additional $100. A 1xBet promo code consists of a combination of letters and numbers that you enter in a designated voucher field to access these advantageous offers.
1xbet promo code philippines
It?s actually a great and useful piece of info. I?m glad that you shared this helpful info with us. Please keep us up to date like this. Thanks for sharing.
amoxicillin 500mg price in canada: AmoHealthPharm – buy cheap amoxicillin online
I am very happy to read this. This is the kind of manual that needs to be given and not the accidental misinformation that is at the other blogs. Appreciate your sharing this best doc.
Aerodrome Finance: Unlocking Potential for Growth
The world of aerodrome finance is pivotal for ensuring the efficient operation, enhancement, and expansion of aerodrome facilities globally. With the increasing demand for air travel, understanding aerodrome financial processes is more important than ever.
aerodrome exchange
Why Aerodrome Finance Matters
Aerodrome finance plays a critical role in the lifespan of airport projects, providing necessary funding from initial development to ongoing management. Here are key reasons why it matters:
Infrastructure Development: Secure financial backing enables the construction and maintenance of essential airport infrastructure.
Operational Efficiency: Adequate funding ensures that airports can operate smoothly, adapting to technological advancements and logistical demands.
Economic Growth: Airports serve as economic hubs; their development stimulates job creation and boosts local economies.
Aerodrome Finance Strategies
Various strategies can be employed to optimize aerodrome finance, ensuring both immediate and long-term benefits. Here are a few notable approaches:
Public-Private Partnerships (PPP)
These partnerships combine public sector oversight and private sector efficiency, leading to shared risks and rewards. They facilitate diverse financial resources and innovative solutions for airport projects.
Revenue Diversification
Exploring non-aeronautical revenue streams, such as retail concessions and property leases, can significantly bolster an airport’s financial resilience. Such diversification allows for a steady income flow independent of ticket sales.
Sustainable Financing
Adopting sustainable financial practices, including green bonds and ESG (Environmental, Social, and Governance) criteria, aligns with modern ecological standards and attracts environmentally conscious investors.
Challenges and Opportunities
While aerodrome finance offers numerous benefits, it also poses certain challenges. High capital costs, regulatory hurdles, and fluctuating passenger demands can impact financial stability. However, these challenges also present opportunities for innovation and improvement.
Tech-Driven Solutions: Embracing technology like AI and predictive analytics can enhance decision-making and financial planning.
Collaboration: Strengthening ties with stakeholders, including airlines and government agencies, can streamline financial operations and capital investments.
Ultimately, the goal of aerodrome finance is to support the sustainable growth and modernization of airports, ensuring their pivotal role in global connectivity remains strong.
clomid without dr prescription: where buy cheap clomid – where can i get cheap clomid price
https://www.panamericano.us/assets/inc/codigo_promocional_bono_de_casino_1xbet.html
The Previous Gods 28 Could 2013 1.10 The Old Gods provides a new Advert 867 begin date and makes most pagans playable with their own unique mechanics.
price of doxycycline: doxycycline medication – buy doxycycline 100mg
amoxicillin 500 mg for sale: order amoxicillin uk – amoxicillin buy canada
canadian pharmacy
http://amohealthpharm.com/# amoxicillin 200 mg tablet
amoxicillin for sale: Amo Health Pharm – can you buy amoxicillin uk
where buy cheap clomid: can you get clomid tablets – how to buy clomid without dr prescription
doxycycline price usa: doxycycline antimalarial – doxycycline 100mg price australia
can i order cheap clomid: can i get generic clomid without a prescription – where buy generic clomid online
amoxicillin medicine: AmoHealthPharm – amoxicillin 500 mg
Balloon Protect : Rise Up 2023 is a very simple but interesting balloon and obstacles game, you have to save the balloon with various dodging interruptions. Be careful to avoid obstacles and make score to the top. This game is very easy to play, but very hard to become master of Balloon Protect : Rise Up 2023.Swing and move shield with finger to protect your balloon while it is rise up(pop). Upstairs in the house. Climb up the containers outside to access the second floor. -Para el globo en vez de puede ser . The gameplay of Balloon Crash is quite simple – players use their mouse or touchscreen to pop balloons as they float across the screen. The player must be quick and accurate in their movements, as missing a balloon will result in a loss of points. Additionally, players must be mindful of special balloons that offer bonus points or power-ups that can help them in their quest to maximize winnings.
http://jobs.emiogp.com/author/daithenabar1981/
Play On Windows PC Frente a la estatua de Abraham Lincoln, Avenida Emilio Castelar 163, Polanco, Ciudad de México, 11560 ¿Qué período de cancelación nos ofrece STIL-Ambiente.de? Sorry, JavaScript must be enabled to use this web app Arcade La defensa de Philadelphia Eagles aplasta a Mahomes para ganar la Super Bowl La última versión de Hot Air Balloon- Balloon Game es 33.1, aparecido en 03 06 2024. Inicialmente fue agregado a nuestra base de datos en 03 06 2024. ¿Cómo poder jugarlo? Para acceder a este juego deberás abrir Google Play cuando no estás conectado a internet. Más abajo observarás “Play while you wait” (juega mientras esperas) y un botón de Play para visualizar el globo. Todos los juegos ¿Cómo poder jugarlo? Para acceder a este juego deberás abrir Google Play cuando no estás conectado a internet. Más abajo observarás “Play while you wait” (juega mientras esperas) y un botón de Play para visualizar el globo.
doxycycline 2985: DoxHealthPharm – doxycycline online canada without prescription
doxycycline 100 mg capsule: DoxHealthPharm – doxycycline over the counter
https://clmhealthpharm.com/# buying clomid for sale
https://zithropharm.shop/# cheap zithromax pills