Friday, June 22, 2012

Convert Nepali Date To English Date in JAVA ( with complete working code snippet )


In my last post, I have explained the detail algorithm for the conversion of Nepali date into English date.
See How To Convert Nepali Date To English.

Here, I will be discussing about the algorithm to convert Nepali date to English. I repeat, English calendar has some fixed pattern for days in a month. But as we know Nepali calendar doesn't have such fixed pattern. That is why we don't have any short quick conversion logic. Step-wise algorithm to convert a Nepali date into an English date has been explained down below. Here also, we still need the list of no. of days in each month of each year within valid range. Here is the list withing the range :

Figure : Nepali Year-Month List


2000 => 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31
2001 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2002 => 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30
2003 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31
2004 => 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31
2005 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2006 => 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30
2007 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31
2008 => 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31
2009 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2010 => 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30
2011 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31
2012 => 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30
2013 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2014 => 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30
2015 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31
2016 => 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30
2017 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2018 => 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30
2019 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31
2020 => 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30
2021 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2022 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30
2023 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31
2024 => 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30
2025 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2026 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31
2027 => 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31
2028 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2029 => 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30
2030 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31
2031 => 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31
2032 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2033 => 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30
2034 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31
2035 => 30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31
2036 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2037 => 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30
2038 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31
2039 => 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30
2040 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2041 => 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30
2042 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31
2043 => 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30
2044 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2045 => 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30
2046 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31
2047 => 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30
2048 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2049 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30
2050 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31
2051 => 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30
2052 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2053 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30
2054 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31
2055 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2056 => 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30
2057 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31
2058 => 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31
2059 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2060 => 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30
2061 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31
2062 => 31, 31, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31
2063 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2064 => 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30
2065 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31
2066 => 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31
2067 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2068 => 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30
2069 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31
2070 => 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30
2071 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2072 => 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30
2073 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31
2074 => 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30
2075 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2076 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30
2077 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31
2078 => 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30
2079 => 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30
2080 => 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30
2081 => 31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30
2082 => 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30
2083 => 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30
2084 => 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30
2085 => 31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30
2086 => 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30
2087 => 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30
2088 => 30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30
2089 => 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30
2090 => 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30


where, 2000, 2001, 2002 and so on are the Nepali years and 30, 32, 31 and so on are the total days in months Baisakh, Jestha, Asadh and so on respectively in the corresponding year.

Now, here's the algorithm to convert English dates to Nepali dates.

# ALGORITHM:

# Step 1: Define the least possible Nepali date from your valid range ( 2000/01/01 Wednesday)
     - Since, in our above list, the least possible valid Nepali year is 2000, let's take the least possible valid Nepali year be 2000/01/01 which is Wednesday according to a Nepali calendar.

# Step 2: Define the equivalent English date ( 1943/04/14 Wednesday)
     -  Here we should find out the actual English date equivalent to Nepali date that we picked up in step 1. You can find the equivalent English date from any Nepali to English date conversion tool, which we can easily find in internet. For my case, I got  1943/04/14.

# Step 3: Pick up a Nepali date you want to convert.

# Step 4: Count the total number of days in between the above two Nepali Dates.
     - Like for English dates, the Java date API like Jodatime, build-in java.util.Calender etc. won't work to directly determine the number of days in between two Nepali dates. You have to devise a logic on your own with the help of above Nepali Year-Month list.. But don't panic, I have already written a method for you.

# Step 5: Add the total number of days (calculated in Step 4.) to the equivalent base English date (calculated in Step 2. ).
     - You can achieve this by simply using java.util.Calendar or any other date/time APIs like JodaTime.

# Step 6: Congratulation, you have the converted English date on your hand.

------------------------------------------------------------------------------------------------------------
Using the following code code snippet, I have developed the Nepali to English and English to Nepali date converter which I have deployed in google app engine. You can see it here : http://dateconverter.appspot.com/ 

Here's the code snippet in JAVA for each step of the above algorithm.

Before going through the steps, let's first create a hashmap the Nepali Year-Months list as follows. We need this list to calculate the no. of days in between two Nepali dates.


// The 0s at index 0 are dummy values so as to make the int array of
// days in months seems more intuitive that index 1 refers to first
// month "Baisakh", index 2 refers to second month "Jesth" and so on. 

Map<Integer, int[]> nepaliMap = new HashMap<Integer, int[]>();

nepaliMap.put(2000, new int[] { 0, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 });
nepaliMap.put(2001, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2002, new int[] { 0, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2003, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 });
nepaliMap.put(2004, new int[] { 0, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 });
nepaliMap.put(2005, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2006, new int[] { 0, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2007, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 });
nepaliMap.put(2008, new int[] { 0, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31 });
nepaliMap.put(2009, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2010, new int[] { 0, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2011, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 });
nepaliMap.put(2012, new int[] { 0, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 });
nepaliMap.put(2013, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2014, new int[] { 0, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2015, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 });
nepaliMap.put(2016, new int[] { 0, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 });
nepaliMap.put(2017, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2018, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2019, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 });
nepaliMap.put(2020, new int[] { 0, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2021, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2022, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 });
nepaliMap.put(2023, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 });
nepaliMap.put(2024, new int[] { 0, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2025, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2026, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 });
nepaliMap.put(2027, new int[] { 0, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 });
nepaliMap.put(2028, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2029, new int[] { 0, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2030, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 });
nepaliMap.put(2031, new int[] { 0, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 });
nepaliMap.put(2032, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2033, new int[] { 0, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2034, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 });
nepaliMap.put(2035, new int[] { 0, 30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31 });
nepaliMap.put(2036, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2037, new int[] { 0, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2038, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 });
nepaliMap.put(2039, new int[] { 0, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 });
nepaliMap.put(2040, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2041, new int[] { 0, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2042, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 });
nepaliMap.put(2043, new int[] { 0, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 });
nepaliMap.put(2044, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2045, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2046, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 });
nepaliMap.put(2047, new int[] { 0, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2048, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2049, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 });
nepaliMap.put(2050, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 });
nepaliMap.put(2051, new int[] { 0, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2052, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2053, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 });
nepaliMap.put(2054, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 });
nepaliMap.put(2055, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2056, new int[] { 0, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2057, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 });
nepaliMap.put(2058, new int[] { 0, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 });
nepaliMap.put(2059, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2060, new int[] { 0, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2061, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 });
nepaliMap.put(2062, new int[] { 0, 31, 31, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31 });
nepaliMap.put(2063, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2064, new int[] { 0, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2065, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 });
nepaliMap.put(2066, new int[] { 0, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31 });
nepaliMap.put(2067, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2068, new int[] { 0, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2069, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 });
nepaliMap.put(2070, new int[] { 0, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 });
nepaliMap.put(2071, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2072, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2073, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 });
nepaliMap.put(2074, new int[] { 0, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2075, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2076, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 });
nepaliMap.put(2077, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 });
nepaliMap.put(2078, new int[] { 0, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2079, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 });
nepaliMap.put(2080, new int[] { 0, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 });
nepaliMap.put(2081, new int[] { 0, 31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30 });
nepaliMap.put(2082, new int[] { 0, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 });
nepaliMap.put(2083, new int[] { 0, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30 });
nepaliMap.put(2084, new int[] { 0, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30 });
nepaliMap.put(2085, new int[] { 0, 31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30 });
nepaliMap.put(2086, new int[] { 0, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 });
nepaliMap.put(2087, new int[] { 0, 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30 });
nepaliMap.put(2088, new int[] { 0, 30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30 });
nepaliMap.put(2089, new int[] { 0, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 });
nepaliMap.put(2090, new int[] { 0, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 });



Step 1: Let's define the least possible Nepali date  2000/01/01 Wednesday.



int startingNepYear = 2000;

int startingNepMonth = 1;

int startingNepDay = 1;

int dayOfWeek Calendar.WEDNESDAY// 2000/1/1 is Wednesday



Step 2: Let's define the equivalent English date  1943/04/14 .


int startingEngYear = 1943;

int startingEngMonth = 4;

int startingEngDay = 14;


Step 3: Let's pickup a Nepali date 2044/05/12 to be converted, which is my birth date by the way.


int nepYear = 2044;

int nepMonth = 5;

int nepDay = 12;


Step 4: Let's calculate the number of days between the two Nepali dates as follows:



long totalNepDaysCount = 0;

// count total days in-terms of year
for (int i = startingNepYear; i < nepYear; i++) {
      for (int j = 1; j <= 12; j++) {
            totalNepDaysCount += nepaliYearsMap.get(i)[j];
      }
}

// count total days in-terms of month
for (int j = startingNepMonth; j < nepMonth; j++) {
      totalNepDaysCount += nepaliYearsMap.get(nepYear)[j];
}

// count total days in-terms of date
totalNepDaysCount += nepDay - startingNepDay;


Step 5: Now, let's add "totalNepDaysCount" to starting base English date. But I don't know why, unfortunately, adding the days in the base English date using java.util.Calendar didn't work for me. That's why I had to write my logic to add the days in an English date as follows:



int[] daysInMonth = new int[] { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int[] daysInMonthOfLeapYear = new int[] { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };




// calculation of equivalent english date...
int engYear = startingEngYear;
int engMonth = startingEngMonth;
int engDay = startingEngDay;

int endDayOfMonth = 0;

while (totalNepDaysCount != 0) {
      if (isLeapYear(engYear)) {
            endDayOfMonth = daysInMonthOfLeapYear[engMonth];
      } else {
            endDayOfMonth = daysInMonth[engMonth];
      }
      engDay++;
      dayOfWeek++;
      if (engDay > endDayOfMonth) {
            engMonth++;
            engDay = 1;
            if (engMonth > 12) {
                  engYear++;
                  engMonth = 1;
            }
      }
      if (dayOfWeek > 7) {
            dayOfWeek = 1;
      }
      totalNepDaysCount--;
}




public static boolean isLeapYear(int year) {
      if (year % 100 == 0) {
            return year % 400 == 0;
      } else {
            return year % 4 == 0;
      }
}



Step 6: Now, you have your required converted English date in variables engYear, engMonth, engDay 
 and dayOfWeek.  In this example, the following results are obtained :



engYear : 1987
engMonth : 8 // August
engDay : 28 
dayOfWeek : 6 // Friday


In this way, you can easily convert any Nepali dates withing the range 2000 <= Nepali date <= 2090 which is equivalent to English dates withing the range  1944 <= English date <= 2033.

---------------------------------------------------------------------------------
Please leave some feedback whether you find this article useful or not. Or even if there is anything I can do to help.

5 comments :

  1. 2062 => 30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31
    should be
    2062 => 31, 31, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31
    as per published calendar.

    ReplyDelete
  2. Hey please post this code in any one of the .NET languages i.e VB.NET or C#.

    Thanks in advance....

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Thanks bro.. Thanks a lot for the code... i was looking for a for a quite long time..

    and hey bro... if possible.. can you publish your contact no. or email address here

    ReplyDelete