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.

Sunday, June 17, 2012

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

So far, if you need to work with English dates, you'll find a lot of date APIs, calender APIs. But unfortunately, we still don't have any of them related to Nepali dates. That is why I am here writing this article. hehe. As you did, I also googled for JAVA code to convert English Dates to Nepali Dates. But I couldn't find it. What I found is just few websites that converts Nepali date to English and vice-versa. But you don't worry, you have this article now. Here, I will be explaining the algorithm to convert English dates into Nepali dates.


Note : You may also want to see : How To Convert Nepali Date To English Date

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 an English date into a Nepali date has been explained down below. Before that, all we need is 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 English date from your valid range ( 1944/01/01 Saturday)
     - Since, in our above list, the least possible valid Nepali year is 2000, the corresponding English year would be 1944. So, let's suppose, the least possible valid English year be 1944/01/01.

# Step 2: Define the equivalent Nepali date ( 2000/09/17 Saturday)
     -  Here we should find out the actual Nepali date equivalent to English date that we picked up in step 1. You can find the equivalent Nepali date from any English to Nepali conversion tool. For my case, I got 2000/09/17.

# Step 3: Pick up an English Date you want to convert.

# Step 4: Count the total number of days in between the above two English Dates.
     - For this you can use any Java API like Jodatime, build-in java.util.Calender or even you can have your own logic to calculate them.

# Step 5: Add the total number of days (calculated in Step 4.) to the equivalent base Nepali date (calculated in Step 2. ) with the help of the above Nepali Year-Month list.
     - The addition of the number of days will be in a loop, one day at a time. Withing the loop, you need to increment the date by one day. For example, after one iteration, the base Nepali date would be 2000/09/18 Sunday, then 2000/09/19 Monday, then 2000/09/20 Tuesday and so on.

# Step 6: Congratulation, you have the converted Nepali 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 : 


// 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 English date 1944/01/01 Saturday.



int startingEngYear = 1944;

int startingEngMonth = 1;

int startingEngDay = 1;

int  dayOfWeek Calendar.SATURDAY; // 1944/1/1 is Saturday



Step 2: Let's define the equivalent Nepali date 2000/09/17.


int startingNepYear = 2000;

int startingNepMonth = 9;

int startingNepDay = 17;


Step 3: Let's pickup the English date 1987/08/28 to be converted, which is my birth date by the way.


int engYear = 1987;

int engMonth = 8;

int engDay = 28;


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



Calendar currentEngDate = new GregorianCalendar();

currentEngDate.set(engYear, engMonth, engDay);


Calendar baseEngDate = new GregorianCalendar();

baseEngDate.set(startingEngYear, startingEngMonth, startingEngDay);

                 

long totalEngDaysCount = daysBetween(baseEngDate, currentEngDate);





public long daysBetween(Calendar startDate, Calendar endDate) {
      Calendar date = (Calendar) startDate.clone();
      long daysBetween = 0;
      while (date.before(endDate)) {
            date.add(Calendar.DAY_OF_MONTH, 1);
            daysBetween++;
      }
      return daysBetween;
} 


Step 5: Now, let's add "totalEngDaysCount" to starting base Nepali date as follows:



// initialize required Nepali date variables with starting Nepali date

int nepYear = startingNepYear;
int nepMonth = startingNepMonth;
int nepDay = startingNepDay;

// decrement totalEngDaysCount until its value becomes 0
while (totalEngDaysCount != 0) {

      // getting the total number of days in month nepMonth in year nepYear    
      int daysInIthMonth = nepaliYearsMap.get(nepYear)[nepMonth];

      nepDay++; // incrementing nepali day

      if (nepDay > daysInIthMonth) {
            nepMonth++;
            nepDay = 1;
      }
      if (nepMonth > 12) {
            nepYear++;
            nepMonth = 1;
      }

      dayOfWeek++; // count the days in terms of 7 days
      if ( dayOfWeek > 7) {
             dayOfWeek = 1;
      }

      totalEngDaysCount--;
}



Step 6: Now, you have your required converted Nepali date in variables nepYear, nepMonth, nepDay 
 and  dayOfWeek . In this example, the following results are obtained :



nepYear : 2044
nepMonth : 5 // Bhadra
nepDay : 12 
dayOfWeek: 6 // Friday


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

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