根据当前日期更改值

时间:2017-04-25 11:02:50

标签: android

我有两个日期的字符串格式,并使用共享首选项存储它。第一个日期(当前日期)设置为日期选择日期,第二个日期是第一个日期添加41天后的日期。我有一个按钮以天为单位来获取两个日期之间的差异。它是使用共享首选项存储的。问题是我必须根据每个当前日期减少这两个日期之间的差异。如何做到这一点?请帮助... < / p>

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import java.util.Calendar;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.concurrent.TimeUnit;

import java.util.concurrent.TimeUnit;

import static com.example.aiswarya.mantraapp.R.id.end;
//import static com.example.aiswarya.mantraapp.R.id.enddate;
import static com.example.aiswarya.mantraapp.R.id.imageView;
import static com.example.aiswarya.mantraapp.R.id.myImageViewText;
//import static com.example.aiswarya.mantraapp.R.id.startdate;
import static com.example.aiswarya.mantraapp.R.id.textView3;

/**
 * Created by aiswarya on 3/24/2017.
 */

public class Fasting extends AppCompatActivity {
    ImageButton backb;
    ImageButton reset;
    TextView startdate,enddate;
    ImageView remaining;
    TextView remainingtext;
    SharedPreferences myprefs;
    public String dayys;
    public  long from,to;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitydate);
        backb=(ImageButton) findViewById(R.id.back);
        reset= (ImageButton) findViewById(R.id.reset);
        // startdate= (TextView) findViewById(R.id.textView2);
        // enddate= (TextView) findViewById(textView3);
        remaining=(ImageView) findViewById(R.id.myImageView);
        //remainingtext=(TextView) findViewById(R.id.myImageViewText);





        backb.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Fasting.this,
                        Home.class);
                startActivity(intent);


            }

        });

        reset.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                startdate.setText("START DATE");
                enddate.setText("END DATE");
                remainingtext.setText("REMAINING DAYS");


            }
        });

        myprefs=getPreferences(MODE_PRIVATE);

        init();


    }



    public void selectFrom(View v) {
        final Calendar today = Calendar.getInstance();

        DatePickerDialog dp = new DatePickerDialog(Fasting.this, new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {


                Log.d("date", dayOfMonth + "/" + month + "/" + year);
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                cal.set(Calendar.MONTH, month);
                cal.set(Calendar.YEAR, year);
                ((TextView)findViewById(R.id.textView2)).setText(cal.get(Calendar.DAY_OF_MONTH) + "-" + (cal.get(Calendar.MONTH)+1) + "-" + cal.get(Calendar.YEAR));
                cal.add(Calendar.DAY_OF_MONTH, 41);

                ((TextView)findViewById(textView3)).setText(cal.get(Calendar.DAY_OF_MONTH) + "-" + (cal.get(Calendar.MONTH)+1) + "-" + cal.get(Calendar.YEAR));



                long from = today.getTimeInMillis();
                long to = cal.getTimeInMillis();
                int days = (int) TimeUnit.MILLISECONDS.toDays(Math.abs(from - to));
                Toast.makeText(getApplicationContext(), "DAYS = " + days, Toast.LENGTH_LONG).show();
                final String dayss=String.valueOf(days);

                remaining.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v){
                        remainingtext.setText(""+dayss+" DAYS");

                    }
                });

            }
        }, today.get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DAY_OF_MONTH));
        dp.show();
    }

    private  void init(){
        startdate=(TextView) findViewById(R.id.textView2);
        enddate=(TextView) findViewById(R.id.textView3);
        remainingtext=(TextView) findViewById(R.id.myImageViewText);


        readPreferences();
    }

    public void onSave(View view) {
        String start=startdate.getText().toString();
        String end=enddate.getText().toString();
        String rem=remainingtext.getText().toString();
        //int ageText=Integer.parseInt(age.getText().toString());

        SharedPreferences.Editor editor=myprefs.edit();
        editor.putString("keyname",start);
        editor.putString("keyage",end);
        editor.putString("keyrem",rem);
        editor.commit();
    }

    public void onReset(View view) {

        SharedPreferences.Editor editor=myprefs.edit();
        editor.clear();
        editor.commit();

        readPreferences();

    }

    public void readPreferences(){
        String stl= myprefs.getString("keyname","");
        startdate.setText(stl);
        //int vall=myprefs.getInt("keyage",0);
        String vall=myprefs.getString("keyage","");
        enddate.setText(String.valueOf(vall));
        String remdays=myprefs.getString("keyrem","");
        remainingtext.setText(String.valueOf(remdays));





    }



}
 

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="com.example.aiswarya.mantraapp.Fasting">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@drawable/linir_back"
    >
    <ImageButton
        android:id="@+id/back"
        android:layout_width="50dp"
        android:layout_marginTop="5dp"
        android:layout_height="30dp"
        android:background="@drawable/linir_back"
        android:src="@drawable/back_button"/>

    <TextView
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="FASTING"
        android:textSize="30dp"
        android:textColor="@color/colorBlue"
        android:textStyle="bold"
        />


</LinearLayout>


<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColorHint="@color/colorAccent"
    android:textStyle="bold"
    android:textColor="@color/colorAccent"
    android:visibility="visible"

    android:onClick="selectFrom"
    android:layout_marginTop="5dp"

    android:text="START DATE"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="10dp"
    android:textColorHint="@color/colorAccent"
    android:textStyle="bold"
    android:textColor="@color/colorAccent"

    android:visibility="visible"
    android:text="END DATE"
    android:textAppearance="?android:attr/textAppearanceMedium" />


<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/submit4resized1"
    android:onClick="onSave"
    android:layout_marginLeft="90dp"
    />


<ImageButton

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"


    android:id="@+id/reset"
    android:src="@drawable/resetbutton"
    android:layout_marginTop="5dp"
    android:background="@color/colorWhite"
    android:layout_marginLeft="70dp"
    android:onClick="onReset" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="216dp">

    <ImageView
        android:id="@+id/myImageView"
        android:layout_width="200dp"
        android:layout_height="150dp"
        android:onClick="showDiff"
        android:src="@drawable/new1"
        android:layout_marginRight="63dp"
        android:layout_marginEnd="63dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:id="@+id/myImageViewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="REMAINING DAYS"
        android:textColor="@color/colorWhite"
        android:layout_marginRight="41dp"
        android:layout_marginEnd="41dp"
        android:layout_alignBottom="@+id/myImageView"
        android:layout_alignRight="@+id/myImageView"
        android:layout_alignEnd="@+id/myImageView"
        android:layout_marginBottom="65dp" />
</RelativeLayout>

0 个答案:

没有答案