markdown-it demo

SimpleDateFormat

import java.text.SimpleDateFormat;

public class Main {
	public static void main(String[] args) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssZ");
		System.out.println(format.format(System.currentTimeMillis()));
		// 20220923194559+0800
		format = new SimpleDateFormat("yyyyMMdd'T'HHmmssZ");
		System.out.println(format.format(System.currentTimeMillis()));
		// 20220923T194559+0800
	}
}

How to convert a string Date to long millseconds

String string_date = "12-December-2012";
SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");
try {
    Date d = f.parse(string_date);
    long milliseconds = d.getTime();
} catch (ParseException e) {
    e.printStackTrace();
}

Period & Duration

Period 是以日期(Date)為基礎,Duration 是以時間(Time)為基礎

時間段表示法: 一年三個月五天六小時七分三十秒內,可以寫成P1Y3M5DT6H7M30S

import java.time.LocalDate;
import java.time.Duration;
import java.time.Period;
import java.time.Instant;

public class Main {
	public static void main(String args[]){
		LocalDate startDate = LocalDate.of(2015, 2, 20);
		LocalDate centerDate = LocalDate.of(2016, 12, 15);
		LocalDate endDate = LocalDate.of(2017, 1, 15);
        
		System.out.println(Period.between(startDate, endDate)); // P1Y10M26D
		System.out.println(Period.between(centerDate, endDate)); // P1M
		System.out.println(Period.parse("P2Y3M5D")); // P2Y3M5D
        
		Instant top = Instant.parse("2017-09-03T10:15:30.00Z");
		Instant start = Instant.parse("2017-10-03T10:15:30.00Z");
		Instant end = Instant.parse("2017-10-03T10:16:30.00Z");
        
		System.out.println(Duration.between(top, start)); // PT720H
		System.out.println(Duration.between(start, end)); // PT1M
		System.out.println(Duration.parse("PT1H2M3S")); // PT1H2M3S
	}
}

循環時間表示法

R【循環次數】【/開始時間】/時間間隔【/結束時間】

從2004年5月6日日本時間下午1點起時間間隔半年零5天3小時循環,且循環3次,可以表示為R3/20040506T130000+09/P0Y6M5DT3H0M0S

1年2個月為循環間隔,無限次循環,最後循環終止於2025年1月1日,可表示為R/P1Y2M/20250101

迴圈印出當月每一天

import java.time.LocalDate;
import java.time.YearMonth;

public class Main {
    public static void main(String[] args) {
        // 獲取當前年月
        YearMonth currentYearMonth = YearMonth.now();
        // 獲取該月的第一天
        LocalDate firstDayOfMonth = currentYearMonth.atDay(1);
        // 獲取該月的最後一天
        LocalDate lastDayOfMonth = currentYearMonth.atEndOfMonth();
        // 使用循環印出每一天
        LocalDate currentDate = firstDayOfMonth;
        while (!currentDate.isAfter(lastDayOfMonth)) {
            System.out.println(currentDate);
            currentDate = currentDate.plusDays(1); // 將日期增加一天
        }
    }
}

        String dateString = "2023-10";
        // 使用 YearMonth.parse 方法將字符串轉換為 YearMonth 對象
        YearMonth currentYearMonth = YearMonth.parse(dateString);

判斷星期

DayOfWeek列舉類型的值是從MONDAY到SUNDAY的,分別對應星期一到星期天。

import java.time.DayOfWeek;
import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        // 獲取當前日期
        LocalDate currentDate = LocalDate.now();
        // 獲取星期幾
        DayOfWeek dayOfWeek = currentDate.getDayOfWeek();
        // 打印結果
        System.out.println("當前日期是星期: " + dayOfWeek);
    }
}

取得當天的開始時間

可以使用 Java 的 LocalDate 類別的 atStartOfDay() 方法,如下所示:

LocalDate today = LocalDate.now();
LocalDateTime startOfDay = today.atStartOfDay();
System.out.println("當天的開始時間: " + startOfDay);
Fork me on GitHub