PersianDate

repository·master·Indexed 43 days ago

https://github.com/samanzamani/persiandate

A Java library for Android designed for Persian (Jalali) calendar conversions, date manipulations, and formatting. Version 1.7.1 provides tools for converting between Gregorian and Jalali dates, parsing date strings via PersianDateFormat, and managing date components.

Tokens
1.5K
Snippets
5
Records
6
Agent score
6%

What's inside PersianDate

  1. Install PersianDate via Gradle

    master

    To use PersianDate in your Android project, follow these two steps in your Gradle configuration:

    1. Add the JitPack repository to your root build.gradle file within the allprojects block:

    2. Add the dependency to your module-level build.gradle file.

    // Step 1: Root build.gradle
    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
    
    // Step 2: Module build.gradle
    dependencies {
        implementation 'com.github.samanzamani:PersianDate:1.7.1'
    }
  2. Format Persian dates with PersianDateFormat

    master

    Use PersianDateFormat to convert a PersianDate object into a formatted string. You can provide a custom pattern to the constructor or use static methods.

    By default, it uses standard characters. To use Farsi numerals, pass PersianDateNumberCharacter.FARSI as the second argument to the constructor or the static format method.

    PersianDate pDate = new PersianDate();
    
    // Using instance methods
    PersianDateFormat pdformater1 = new PersianDateFormat();
    pdformater1.format(pDate);
    
    PersianDateFormat pdformater2 = new PersianDateFormat('y F j');
    // Return Farsi characters
    PersianDateFormat pdformater3 = new PersianDateFormat('y F j', PersianDateNumberCharacter.FARSI);
    
    // Using static methods
    PersianDateFormat.format(pDate, 'y F j');
    PersianDateFormat.format(pDate, 'y F j', PersianDateNumberCharacter.FARSI);
  3. Manage and manipulate PersianDate objects

    master

    The PersianDate class allows you to create, manipulate, and query Jalali (Persian) and Gregorian dates.

    Creation

    • new PersianDate(): Current date/time.
    • new PersianDate(Long timestamp): Create from a timestamp.
    • new PersianDate(Date date): Create from a java.util.Date object.

    Manipulation

    • setShYear(int), setShMonth(int), setShDay(int): Set Jalali components.
    • setGrgYear(int), setGrgMonth(int), setGrgDay(int): Set Gregorian components.
    • addDate(): Add time to the object.
    • addWeeks(int), addDays(int), addMonths(int), addYears(int), addHours(int), addMinutes(int): (v1.7.1+) Specific addition methods.
    • subsWeeks(int), subsDays(int), subsMonths(int), subsYears(int), subsHours(int), subsMinutes(int): (v1.7.1+) Specific subtraction methods.

    Conversion and Comparison

    • toJalali(int year, int month, int day): Convert Gregorian to Jalali (returns int[3]).
    • toGregorian(int year, int month, int day): Convert Jalali to Gregorian (returns int[3]).
    • toDate(): Convert to java.util.Date object.
    • after(PersianDate), before(PersianDate), equals(PersianDate): Compare dates.

    Information Retrieval

    • getShYear(), getShMonth(), getShDay(): Get Jalali components.
    • getGrgYear(), getGrgMonth(), getGrgDay(): Get Gregorian components.
    • dayOfWeek(): Returns 0-6 (0=Saturday).
    • dayName(): Returns Persian day name (e.g., شنبه).
    • monthName(): Returns Persian month name (e.g., فروردین).
    • isLeap(): Check if Jalali year is leap.
    • grgIsLeap(): Check if Gregorian year is leap.
  4. Parse date strings with PersianDateFormat

    master

    You can convert string representations of dates back into PersianDate objects using parse or parseGrg methods.

    PersianDateFormat pdformater = new PersianDateFormat();
    // Convert Jalali string (e.g., '1396-05-05') using a pattern
    PersianDate pDate = pdformater.parse("1396-05-05", "yyyy-MM-dd");
    
    // Convert Gregorian string to PersianDate
    PersianDate pDateGrg = pdformater.parseGrg("2017-07-27", "yyyy-MM-dd");
  5. Reference: PersianDateFormat parse keys

    master

    When parsing date strings, use these keys to define the input pattern:

    | String key | Role |
    | ------------- | ----- |
    | yyyy | Year (1396-2012-...) |
    | MM | Month number (12-01-02-...) |
    | dd | Day number (21-01-02-...) |
    | HH | Hour (21-01-02-...) |
    | mm | Minute (21-01-02-...) |
    | ss | Second (21-01-02-...) |
  6. Reference: PersianDateFormat pattern keys

    master

    When using PersianDateFormat, use the following pattern keys to define the output format:

    | String key | Role |
    | ------------- | ----- |
    | l | Day name in week (شنبه و ....) |
    | j | Day number in month (1-10-20...) |
    | F | Month name (فروردین) |
    | Y | Year (1396...) |
    | H | Hour in day |
    | i | Minutes in hour |
    | s | Second in minute |
    | d | day in month (01-02-21...) |
    | g | Hour in day 1-12 |
    | n | Month of year 1-12 |
    | m | Month of year 01-12 |
    | t | number day of month |
    | w | day in week 0-6 |
    | y | year with 2 digits |
    | z | Number of days (full) past the year |
    | L | Is leap year (0-1) |