专家,试图展示Android Studio中两个日期之间的区别。有以下条件。
以下条件第一号工作正常。 1.当前日期与未来日期之间的差异
条件2有问题,它显示了值,但没有显示负号,以了解它已经通过。
主要活动。
package com.bar.example.myapplication;
import android.app.DialogFragment;
//import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
private EditText editTextDate1, editTextDate2, num1, num2;
private TextView txtResult, tv, textDivNumber, textAVG, txtZaMisiac;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextDate1 = (EditText) findViewById(R.id.editTextDate1);
editTextDate2 = (EditText) findViewById(R.id.editTextDate2);
num1 = (EditText) findViewById(R.id.num1);
num2 = (EditText) findViewById(R.id.num2);
button = (Button) findViewById(R.id.button);
txtResult = (TextView) findViewById(R.id.txtResult);
textDivNumber = (TextView) findViewById(R.id.textDivNumber);
textAVG = (TextView) findViewById(R.id.textAVG);
txtZaMisiac = (TextView) findViewById(R.id.txtZaMisiac);
tv = (TextView) findViewById(R.id.tv);
tv.setText("Current date " + (DateFormat.format("dd.MM.yyyy", new java.util.Date()).toString()));
editTextDate1.setText(DateFormat.format("dd.MM.yyyy", new java.util.Date()).toString());
}
public void diff(View view) {
SimpleDateFormat myFormat = new SimpleDateFormat("dd.MM.yyyy");
String inputString1 = editTextDate1.getText().toString();
String inputString2 = editTextDate2.getText().toString();
String number11 = num1.getText().toString();
String number12 = num2.getText().toString();
String[] isbnParts = inputString1.split("\\.");
if (Integer.parseInt(isbnParts[0]) > 31) {
Toast.makeText(this, "It looks like you've typed too many days in a month", Toast.LENGTH_SHORT).show();
}
if (Integer.parseInt(isbnParts[1]) > 12) {
Toast.makeText(this, "Gm ... introduced a non-existent month)", Toast.LENGTH_SHORT).show();
}
String[] isbnParts1 = inputString2.split("\\.");
if (Integer.parseInt(isbnParts1[0]) > 31) {
Toast.makeText(this, "Hey ... too many days in a month", Toast.LENGTH_SHORT).show();
}
if (Integer.parseInt(isbnParts1[1]) > 12) {
Toast.makeText(this, "Hmm ... you entered a non-existent month)", Toast.LENGTH_SHORT).show();
}
if (inputString1.matches("") || inputString2.matches("")) {
Toast.makeText(this, "No dates entered", Toast.LENGTH_SHORT).show();
} else {
try {
Date date1 = myFormat.parse(inputString1);
Date date2 = myFormat.parse(inputString2);
long diffDate = Math.abs(date2.getTime() - date1.getTime());
txtResult.setText("" + TimeUnit.DAYS.convert(diffDate, TimeUnit.MILLISECONDS));
} catch (ParseException e) {
e.printStackTrace();
}
if (number11.matches("") || number12.matches("")) {
Toast.makeText(this, "No numbers entered for calculation", Toast.LENGTH_SHORT).show();
} else {
String pattern = "##0.00";
String pattern1 = "##0";
DecimalFormat decimalFormat = new DecimalFormat(pattern);
DecimalFormat decimalFormat1 = new DecimalFormat(pattern1);
float number1 = Integer.parseInt(number11);
float number2 = Integer.parseInt(number12);
float diffDateInt = Integer.parseInt(txtResult.getText().toString());
float diffnumb = Math.abs(number1 - number2);
String format3 = decimalFormat1.format(diffnumb);
textDivNumber.setText(String.valueOf(format3));
String format = decimalFormat.format((diffnumb / diffDateInt) * 30.00);
txtZaMisiac.setText(String.valueOf(format));
String format1 = decimalFormat.format(diffnumb / diffDateInt);
textAVG.setText(String.valueOf(format1));
}
}
}
public void setDate1(View view) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "Date Picker");
}
public void setDate2(View view) {
DialogFragment newFragment = new DatePickerFragment2();
newFragment.show(getFragmentManager(), "Date Picker");
}
}
DatePickerFragment.java
package com.bar.example.myapplication;
import android.app.DatePickerDialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.DatePicker;
import android.app.Dialog;
import java.util.Calendar;
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
EditText editTextDate1 = (EditText) getActivity().findViewById(R.id.editTextDate1);
String stringOfDate = day + "." + (month + 1) + "." + year;
editTextDate1.setText(stringOfDate);
}
}
DatePickerFragment2.java
package com.bar.example.myapplication;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.EditText;
import java.util.Calendar;
public class DatePickerFragment2 extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
EditText editTextDate2 = (EditText) getActivity().findViewById(R.id.editTextDate2);
String stringOfDate = day + "." + (month + 1) + "." + year;
editTextDate2.setText(stringOfDate);
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:background="#B3E5FC"
tools:context=".MainActivity"
>
<!--This example works only date picker mode = spinner mode only-->
<!--DatePicker with spinner and without Calendar-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="set date 1"
android:id="@+id/btnDate1" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
android:layout_marginTop="42dp" android:onClick="setDate1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="set date 2"
android:id="@+id/btnDate2" android:layout_alignTop="@+id/btnDate1" android:layout_alignParentRight="true"
android:onClick="setDate2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="calculate"
android:id="@+id/button"
android:onClick="diff" android:layout_centerVertical="true" android:layout_centerHorizontal="true"
android:textColor="#f21124"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editTextDate1" android:layout_below="@+id/btnDate1" android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/btnDate1"
android:inputType="date" android:textColor="#9e0e1f" android:textSize="15dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editTextDate2" android:layout_below="@+id/btnDate2"
android:layout_alignRight="@+id/btnDate2" android:layout_alignLeft="@+id/btnDate2"
android:inputType="date" android:textColor="#e11515" android:textSize="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/txtResult"
android:text="............"
android:textSize="14dp" android:layout_below="@+id/button" android:layout_alignLeft="@+id/num2"
android:layout_alignStart="@+id/num2" android:textColor="#5c2e2e"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/num1" android:layout_below="@+id/editTextDate1" android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/button" android:layout_alignRight="@+id/editTextDate1"
android:layout_alignEnd="@+id/editTextDate1" android:textColor="#590e9b" android:textSize="15dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/num2" android:layout_below="@+id/editTextDate2" android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" android:layout_alignLeft="@+id/editTextDate2"
android:layout_alignStart="@+id/editTextDate2" android:textColor="#3818ad" android:textSize="15dp"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/txtResult"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Between dates"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#060606"
android:textSize="14dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView"
android:text="The difference between the numbers"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#070707"
android:textSize="14dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="............"
android:id="@+id/textDivNumber"
android:layout_below="@+id/txtResult" android:layout_alignLeft="@+id/txtResult"
android:layout_alignStart="@+id/txtResult" android:textColor="#782f2f" android:textSize="14dp"/>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView2"
android:text="Average value"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#0a0a0a"
android:textSize="14dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="............"
android:id="@+id/textAVG"
android:layout_below="@+id/textDivNumber" android:layout_alignLeft="@+id/textDivNumber"
android:layout_alignStart="@+id/textDivNumber" android:textColor="#7e3333" android:textSize="14dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/tv"
android:layout_alignParentTop="true" android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textDivNumber"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="days"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignStart="@+id/textView3"
android:layout_below="@+id/textDivNumber"
android:text="in a day"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="10dp" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/txtZaMisiac"
android:text="Per month"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#171414"
android:textSize="14dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="............"
android:id="@+id/txtZaMisiac"
android:layout_below="@+id/textView5" android:layout_alignLeft="@+id/textAVG"
android:layout_alignStart="@+id/textAVG" android:textSize="14dp" android:textColor="#052df7"/>
</RelativeLayout>
新差异代码
public void diff(View view) {
// SimpleDateFormat myFormat = new SimpleDateFormat("dd.MM.yyyy");
// String inputString1 = ok.getText().toString();
// String inputString2 = ok1.getText().toString();
CharSequence inputString1 = editTextDate1.getText();
try {
LocalDate date1 = LocalDate.parse(inputString1, dateFormatter);
CharSequence inputString2 = editTextDate2.getText();
try {
LocalDate date2 = LocalDate.parse(inputString2, dateFormatter);
long diffDate = ChronoUnit.DAYS.between(date1, date2);
txtResult.setText(String.valueOf(diffDate));
} catch (DateTimeParseException dtpe) {
Toast.makeText(this, "Date2 is not a valid date: " + inputString2, Toast.LENGTH_SHORT).show();
}
} catch (DateTimeParseException dtpe) {
Toast.makeText(this, "Date1 is not a valid date: " + inputString1, Toast.LENGTH_SHORT).show();
}
// if (number11.matches("")||number12.matches("")){Toast.makeText(this, "No numbers entered for calculation", Toast.LENGTH_SHORT).show();}
}
&#13;
答案 0 :(得分:1)
让我们首先声明一个格式化程序。这可能是一个静态字段:
private static DateTimeFormatter dateFormatter
= DateTimeFormatter.ofPattern("d.M.uuuu");
然后在diff
中的MainActivity
方法中,您可能会执行以下操作:
CharSequence inputString1 = editTextDate1.getText();
try {
LocalDate date1 = LocalDate.parse(inputString1, dateFormatter);
CharSequence inputString2 = editTextDate2.getText();
try {
LocalDate date2 = LocalDate.parse(inputString2, dateFormatter);
long diffDate = ChronoUnit.DAYS.between(date1, date2);
txtResult.setText(String.valueOf(diffDate));
} catch (DateTimeParseException dtpe) {
Toast.makeText(this, "Date2 is not a valid date: " + inputString2, Toast.LENGTH_SHORT).show();
}
} catch (DateTimeParseException dtpe) {
Toast.makeText(this, "Date1 is not a valid date: " + inputString1, Toast.LENGTH_SHORT).show();
}
5.5.2018
和31.5.2018
,则会显示26
的差异。5.5.2018
和1.5.2018
,则会显示-4
的差异。我相信这条线是罪魁祸首:
long diffDate = Math.abs(date2.getTime() - date1.getTime());
Math.abs()
获取参数的绝对值,即将负数转换为正数。所以无论日期是过去还是将来,你都会得到一个正数。从毫秒转换为天后,您仍然有一个正数,因此它显示没有符号。
也就是说,您使用的日期和时间类Calendar
,Date
和SimpleDateFormat
已经过时且设计不佳。 SimpleDateFormat
特别是出了名的麻烦。考虑避免它们并使用java.time
,即现代Java日期和时间API。
是的,java.time
适用于较旧和较新的Android设备。它至少需要 Java 6 。
org.threeten.bp
和子包中导入日期和时间类。java.time
。