如何使用PersianDate库获取波斯月份名称并处理月份时间戳
Hey there! Since you're a beginner working with the PersianDate library, let's break down how to tackle your requirements step by step, replacing the old Calendar code and adding the features you need.
1. Rewrite getMonthTimestamp for Persian Months
Your original method gets the start of the Gregorian month for a given timestamp. If you want the start of the Persian month (which I assume is why you're using this library), here's how to do it with PersianDate:
private long getPersianMonthStartTimestamp(long timestamp) { // Initialize PersianDate with your input timestamp PersianDate persianDate = new PersianDate(timestamp); // Set to the first day of the Persian month persianDate.setDay(1); // Set time to midnight (00:00:00.000) persianDate.setHour(0); persianDate.setMinute(0); persianDate.setSecond(0); persianDate.setMillisecond(0); // Return the adjusted timestamp return persianDate.getTimeInMillis(); }
This method will give you the Unix timestamp for the start of the Persian month that contains your input timestamp. The library handles all Gregorian-Persian conversions under the hood, so you don't have to do manual calculations.
2. Generate a List of Persian Month Names
The library has a built-in static method to get all Persian month names. Creating a list is straightforward:
import java.util.Arrays; import java.util.List; // Get the full list of Persian month names List<String> persianMonths = Arrays.asList(PersianDate.getMonthNames()); // Print them out to verify (optional) for (String month : persianMonths) { System.out.println(month); }
This will output the 12 Persian months in order: فروردین, اردیبهشت, خرداد, تیر, مرداد, شهریور, مهر, آبان, آذر, دی, بهمن, اسفند.
3. Check if There Are Transactions in the Current Persian Month
To check for transactions in the current Persian month, we'll get the start of the current month, the start of the next month, then see if any transaction timestamps fall between these two values:
// Step 1: Get start timestamp of current Persian month PersianDate currentPersianDate = new PersianDate(); // Uses current time by default currentPersianDate.setDay(1); currentPersianDate.setHour(0); currentPersianDate.setMinute(0); currentPersianDate.setSecond(0); currentPersianDate.setMillisecond(0); long currentMonthStart = currentPersianDate.getTimeInMillis(); // Step 2: Get start timestamp of the next Persian month PersianDate nextPersianDate = new PersianDate(currentMonthStart); nextPersianDate.addMonth(1); // Safely moves to the start of the next month long nextMonthStart = nextPersianDate.getTimeInMillis(); // Step 3: Check your transaction timestamps (replace with your actual data) List<Long> transactionTimestamps = ...; // Your list of transaction timestamps boolean hasTransactionsThisMonth = false; for (long transactionTime : transactionTimestamps) { if (transactionTime >= currentMonthStart && transactionTime < nextMonthStart) { hasTransactionsThisMonth = true; break; } } // Step 4: Use the result if (hasTransactionsThisMonth) { System.out.println("There are transactions in the current Persian month!"); } else { System.out.println("No transactions found in the current Persian month."); }
Quick Tips for Beginners
- Double-check that you've added the library dependency correctly in your build file (Gradle/Maven).
- The
addMonth()method handles edge cases like transitioning from Esfand (the last Persian month) to Farvardin (the first) automatically. - If you ever need to convert between Persian and Gregorian dates explicitly, use
persianDate.toGregorian()ornew PersianDate(gregorianDate).
内容的提问来源于stack exchange,提问作者M khorsand

