Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.joythis.android.datesdistance;
- import java.util.Calendar;
- /*
- objetivo:
- - ajudar na matemática entre datas
- - fornecer frases correspondentes a datas
- */
- public class AmDate {
- public static int MES_IMPOSSIVEL = -1;
- int mYear;
- int mMonth;
- int mDay;
- /*
- default constructor
- vai representar a "data corrente"
- conforme o relógio do
- dispositivo móvel
- */
- public AmDate(){
- Calendar c =
- Calendar.getInstance();
- int y = c.get(Calendar.YEAR);
- /*
- A Java Calendar is 0-based
- for the month
- 0 - January
- ...
- 10 - November
- 11 - December
- */
- int m = c.get(Calendar.MONTH);
- int d = c.get(Calendar.DAY_OF_MONTH);
- this.mYear = y;
- this.mMonth = m+1; // natural month
- this.mDay = d;
- }//AmDate
- public String toString(){
- String s = String.format(
- "%4d-%2d-%2d",
- this.mYear,
- this.mMonth,
- this.mDay
- );
- return s;
- }//toString
- public static boolean bValidYear(
- int pYear
- ){
- return pYear>=1;
- }//bValidYear
- public static boolean bValidMonth(
- int pMonth
- ){
- return pMonth>=1 && pMonth<=12;
- }//bValidMonth
- public static boolean bValidDate(
- int pY, int pM, int pD
- ){
- boolean bYearOK = bValidYear(pY);
- boolean bMonthOK = bValidMonth(pM);
- boolean bDayOK =
- pD>=1
- &&
- pD<=iHowManyDaysIn(pM, pY);
- return bYearOK && bMonthOK && bDayOK;
- }//bValidDate
- public boolean leapYear(int pY){
- boolean bM400 = pY%400==0;
- boolean bM4NotSecular =
- pY%4==0 && pY%100!=0;
- return bM400 || bM4NotSecular;
- }//leapYear
- public static int iHowManyDaysIn
- (int pM, int pY)
- {
- boolean bCase31 =
- pM==1 || pM==3 || pM==5 || pM==7
- || pM==8 || pM==10 || pM==12;
- if (bCase31) return 31;
- boolean bCase30 =
- pM==4 || pM==6 || pM==9 || pM==11;
- if(bCase30) return 30;
- if(pM==2)
- return leapYear(pY)?29:28;
- return MES_IMPOSSIVEL;
- }//iHowManyDaysIn
- }//AmDate
- ********************
- //MainActivity.java
- package com.joythis.android.datesdistance;
- import androidx.appcompat.app.AppCompatActivity;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- public class MainActivity extends AppCompatActivity {
- Activity mA;
- EditText mEtDate1, mEtDate2;
- Button mBtnPickDate1,
- mBtnPickDate2,
- mBtnComputeDistance;
- View.OnClickListener mClickHandler = new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- switch(v.getId()){
- case R.id.idBtnPickDate1:
- actionPickDate1();
- break;
- case R.id.idBtnPickDate2:
- actionPickDate2();
- break;
- }//switch
- }//onClick
- };//mClickHandler
- void actionPickDate1(){
- //SAFR!!! TODO
- Intent goGoPickDate1 = new Intent(
- MainActivity.this,
- DateInput.class
- );
- startActivity(goGoPickDate1);
- }//actionPickDate1
- void actionPickDate2(){
- //SAFR!!! TODO
- Intent goGoPickDate2 = new Intent(
- MainActivity.this,
- DateInput.class
- );
- startActivity(goGoPickDate2);
- }//actionPickDate2
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.rl_distance_dates);
- init();
- }//onCreate
- void init(){
- //1 - assocs
- mEtDate1 = findViewById(R.id.idEtDate1);
- mEtDate2 = findViewById(R.id.idEtDate2);
- mBtnPickDate1 = findViewById(R.id.idBtnPickDate1);
- mBtnPickDate2 = findViewById(R.id.idBtnPickDate2);
- mBtnComputeDistance = findViewById(R.id.idBtnComputeDistance);
- displayDefaultDates();
- //2 - behaviors
- mBtnPickDate1.setOnClickListener(mClickHandler);
- mBtnPickDate2.setOnClickListener(mClickHandler);
- }//init
- void displayDefaultDates(){
- AmDate d = new AmDate();
- mEtDate1.setText(d.toString());
- mEtDate2.setText(d.toString());
- }//displayDefaultDates
- }
- ********************************
- <?xml version="1.0" encoding="utf-8"?>
- <!-- rl_pick_date.xml -->
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:id="@+id/idLlLabels"
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:gravity="center_horizontal"
- android:layout_weight="1"
- android:text="@string/strTvYear"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <TextView
- android:gravity="center_horizontal"
- android:layout_weight="1"
- android:text="@string/strTvMonth"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <TextView
- android:gravity="center_horizontal"
- android:layout_weight="1"
- android:text="@string/strTvDay"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- <LinearLayout
- android:id="@+id/idLlInputs"
- android:layout_below="@id/idLlLabels"
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <NumberPicker
- android:layout_weight="1"
- android:id="@+id/idNpYear"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <NumberPicker
- android:layout_weight="1"
- android:id="@+id/idNpMonth"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <NumberPicker
- android:layout_weight="1"
- android:id="@+id/idNpDay"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- <Button
- android:layout_below="@id/idLlInputs"
- android:id="@+id/idBtnConfirm"
- android:text="@string/strBtnConfirm"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <Button
- android:layout_below="@id/idBtnConfirm"
- android:id="@+id/idBtnCancel"
- android:text="@string/strBtnCancel"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </RelativeLayout>
- ****************************************************************
- //DateInput.java
- package com.joythis.android.datesdistance;
- import androidx.appcompat.app.AppCompatActivity;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.Button;
- import android.widget.NumberPicker;
- import java.util.Calendar;
- public class DateInput extends AppCompatActivity {
- Activity mA;
- NumberPicker mNpYear, mNpMonth, mNpDay;
- Button mBtnConfirm, mBtnCancel;
- NumberPicker.OnValueChangeListener mNpValueHandler =
- new NumberPicker.OnValueChangeListener() {
- @Override
- public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
- int y = mNpYear.getValue();
- int m = mNpMonth.getValue();
- int maxDay = AmDate.iHowManyDaysIn(y, m);
- mNpDay.setMaxValue(maxDay);
- }//onValueChange
- };//mNpValueHandler
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.rl_pick_date);
- init();
- }//onCreate
- void initNpDate(){
- int iCurrentYear = Calendar.getInstance().get(Calendar.YEAR);
- mNpYear.setMinValue(iCurrentYear-400);
- mNpYear.setMaxValue(iCurrentYear+100);
- mNpYear.setValue(iCurrentYear);
- int iCurrentMonth = Calendar.getInstance().get(Calendar.MONTH)+1;
- mNpMonth.setMinValue(1);
- mNpMonth.setMaxValue(12);
- mNpMonth.setValue(iCurrentMonth);
- int iCurrentDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
- mNpDay.setMinValue(1);
- mNpDay.setMaxValue(AmDate.iHowManyDaysIn(iCurrentYear, iCurrentMonth));
- mNpDay.setValue(iCurrentDay);
- }//initNpDate
- void init(){
- //1 - assocs
- mNpYear = findViewById(R.id.idNpYear);
- mNpMonth = findViewById(R.id.idNpMonth);
- mNpDay = findViewById(R.id.idNpDay);
- NumberPicker[] nps = {mNpYear, mNpMonth, mNpDay};
- for (NumberPicker np : nps){
- np.setOnValueChangedListener(mNpValueHandler);
- }//for
- initNpDate();
- //2 - behaviors
- }//init
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement