gregorianToEthiopianString static method

String gregorianToEthiopianString(
  1. String gregorianDateStr, {
  2. String separator = '/',
})

Convert Gregorian date string to Ethiopian date string Input format: "day/month/year" or "day-month-year" Returns: Formatted Ethiopian date string

Implementation

static String gregorianToEthiopianString(String gregorianDateStr, {String separator = '/'}) {
  try {
    // Validate format first
    if (!_isValidDateFormat(gregorianDateStr)) {
      throw Exception('Invalid date format. Use day/month/year or day-month-year with proper separators');
    }

    // Support both / and - separators
    final parts = gregorianDateStr.contains('/')
        ? gregorianDateStr.split('/')
        : gregorianDateStr.split('-');

    if (parts.length != 3) {
      throw Exception('Invalid date format. Use day/month/year or day-month-year');
    }

    final day = int.parse(parts[0]);
    final month = int.parse(parts[1]);
    final year = int.parse(parts[2]);

    // Validate Gregorian date ranges
    if (month < 1 || month > 12) {
      throw Exception('Month must be between 1 and 12');
    }
    if (day < 1 || day > 31) {
      throw Exception('Day must be between 1 and 31');
    }

    final result = gregorianToEthiopian(year: year, month: month, day: day);

    return '${result['day']}$separator${result['month']}$separator${result['year']}';
  } catch (e) {
    throw Exception('Date conversion failed: $e');
  }
}