keyrunHORNET / date_picker_converter

Nepali Date Picker with Date conversion from BS to AD and vice-versa
Apache License 2.0
36 stars 23 forks source link
android-library bikram-samwat-converter conversion dateconvert datetimepicker java material material-design nepali nepali-calendar nepali-datepicker nepali-year nepalidateconverter picker picker-converter

Nepali Date picker Converter

This work is the derivative of Material Date Time Picker, in the original work, calender of a date picker is based on the Gregorian Calendar, but this tries to offer you a calendar based on Bikram Sambat. The library uses the code from the Material Date Time Picker as a base and tweaked it to fill the need for Nepali Calendar System with the extra feature of converting Gregorian(AD) date to Nepali(BS) and Vice versa.

Contributor

Jeffrey Jongko

Date Picker Calendar
Date Picker Calendar

demo (old version)

https://play.google.com/store/apps/details?id=com.hornet.nepalidateconverter

Setup

The easiest way:

Step 1. Add the JitPack repository to your build file. Add it in your root build.gradle at the end of repositories:


   allprojects {
         repositories {
              ...
              maven { url "https://jitpack.io" }
         }
    }

Step 2. Add the dependency


 dependencies {
           implementation 'com.github.keyrunHORNET:date_picker_converter:$latest_version'
    }

You may also add the library as an Android Library to your project. All the library files live in library.

Using Calendar

include the calendar in your desired layout

 <com.hornet.dateconverter.CalendarView.Calendar
        android:id="@+id/calendar"
        android:layout_width="wrap_content"
        android:layout_height="300dp" />

In order to receive the date checked in the calendar, you will need to implement the CalendarView.Calendar.OnDateSetListener interface. This will be a the Activity or Fragment that creates the calendar in their respective layout. Then hook up the interface with calendar in your layout.

calendar.setOnDateSetListener(this);

@Override
   public void onDateClick(View calendar, int year, int month, int day) {
        Toast.makeText(this, "year :: " + year + "  month :: " + (month + 1) + " day :: " + day, Toast.LENGTH_SHORT).show();
   }

Using Date Picker / Time Picker

Implement an OnDateSetListener / OnTimeSetListener

In order to receive the date set in the picker, you will need to implement the OnDateSetListener interfaces. Typically this will be the Activity or Fragment that creates the Pickers. The callbacks use the same API as the standard Android pickers.


@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
  String date = "You picked the following date: "+dayOfMonth+"/"+(monthOfYear+1)+"/"+year;
  dateTextView.setText(date);
}

@Override
public void onTimeSet(RadialPickerLayout view, int hour, int minute, int second) {
  String time = "You picked the following time:- "+hour+":"+minute+":"+second;
  timeTextView.setText(time);
}

Create a DatePickerDialog / TimePickerDialog using the supplied factory

You will need to create a new instance of DatePickerDialog using the static newInstance() method, supplying proper default values and a callback. Once the dialogs are configured, you can call show().

DatePickerDialog dpd = DatePickerDialog.newInstance(MainActivity.this);
dpd.show(getFragmentManager(), "Datepickerdialog");

Using Date Converter

DateConverter dc=new DateConverter();

year month day when passed beyond the conversion range throws an IllegalArgumentException.

Both of the above method returns the model of date. Model is a DTO with getter and setter for year month and day .

Example:

Model outputOfConversion=dc.getEnglishDate(nepYY,nepMM,nepDD);

int year=outputOfConversion.getYear();
int month=outputOfConversion.getMonth(); 
int day=outputOfConversion.getDay();

Additional Options

FAQ

Why Date Model instead of Java Calendar

In java Calendar class DAY_OF_MONTH cant be assigned to 32, and throws an IllegalArgumentError,but in Bikram Sambat Calendar system we have DAY_OF_MONTH with the value of 32 in several month.

Why does the DatePickerDialog and DateConverter return the selected month -1?

In the java Calendar class months use 0 based indexing: January is month 0, December is month 11. This convention is widely used in the java world, for example the native Android DatePicker.And in Nepali Calendar Baisakh is month 0, Chaitra is month 11.

License

Copyright (c) 2016 Kiran Gyawali

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Heads Up