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.

10 comments:

  1. Great MAN ........ Really Helpful... Thanks.

    ReplyDelete
  2. Thanks Manoj for this great article. Could you please extend JGoodie's JDateChooser component with Nepali Date Support ?

    ReplyDelete
  3. hey can anyone upload the dame in C# for .NET framework

    ReplyDelete
  4. Hello Mr. Manoj Shrestha
    I found your blog helpful, thanks for the code.
    I tried your code just to check whether it works fine for all dates.

    I found didn't get correct date conversion of year YYYY/MM/DD 1987/2/19. Just try with same month and year by changing day. It doesn't work fine for 1987/2.

    It works fine on http://dateconverter.appspot.com/

    Just find out the problem and let me know.

    There is one error, i.e instead of nepaliMap you have used nepaliYearsMap
    // getting the total number of days in month nepMonth in year nepYear
    int daysInIthMonth = nepaliYearsMap.get(nepYear)[nepMonth];

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

    ReplyDelete
  6. Ok fine i solved this error
    I have another Problem i want to use Nepali date in Text field of Java Netbeans How to use it Please Tell me about this.Really Required this.Plz help me if you know how to do...

    ReplyDelete
  7. hey this code is not accurate for today's date i.e. 2013-09-17. shows equivalent nepali date as bhadra 31 2070 instead of 2070 aswin 1

    ReplyDelete
  8. hi Manoj, it is really great , i'd appreciate if you provide some idea to create own nepali calendar

    thanks again

    ReplyDelete
  9. there is error for 2044.11.10 with english date according to rajan calender english date is feb,22, 1988

    ReplyDelete