gpt4 book ai didi

java - 如何比较两个java sql日期,一个在列表中,另一个刚刚输入

转载 作者:行者123 更新时间:2023-11-30 02:06:37 25 4
gpt4 key购买 nike

我已经尝试过这里发布的许多问题。我仍然没有找到解决问题的方法,

我想比较用户输入的日期,在我的 android 应用程序中,我将日期存储在 ArrayList 中。

我不想将克隆存储到已保存的日期,所以我必须检查一下。我正在使用 Java sql 日期来避免时间部分。

static List<DatefromTo> myList;
myList = new ArrayList<Dateclassobject>();
i =myList.size();

for(int j = 1; j <= myList.size(); j ++){
int results2 = myList.get(i).datefrom.compareTo(myList.get(j).datefrom);
if(results2 > 0 ){
Toast.makeText(getApplicationContext(),"First Date is after second",Toast.LENGTH_LONG).show();
}else if(results2 < 0){
Toast.makeText(getApplicationContext(), "Wrong", Toast.LENGTH_LONG).show();
}else if(results2 == 0){
Toast.makeText(getApplicationContext(),String.valueOf(myList.get(i).datefrom) + "------" + String.valueOf(myList.get(j).datefrom),Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Hi", Toast.LENGTH_LONG).show();
}
}

datefromdateto存储在Dateclassobjecteveryobject

所以我想要实现的是,如果用户输入了从 datefromdateto 的时间段,并且该时间段已经存在于另一个时间段或者是存在于列表本身中,它不会再次输入。那么我怎样才能最好地检查呢?

另外我想知道为什么getYear(), 或者getDate() 给出错误的答案?


这是 DateClassObject 代码:

import java.util.Date;

public class DateClassObject {

public static Date datefrom;
public static Date dateto;

public static Date difference;

public DateClassObject() {
super();
}

@SuppressWarnings("static-access")
public DateClassObject(Date datefrom, Date dateto) {
super();

this.datefrom = datefrom;
this.dateto = dateto;
}
}

下面是这个类 Dateadd 的全部代码,我在这里做所有事情

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;

//import java.util.Date;

import java.util.List;


import android.app.Activity;
import android.app.DatePickerDialog;
import android.graphics.Color;
import android.os.Bundle;

import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;

import android.widget.TextView;
import android.widget.Toast;

public class DateAdd extends Activity {
private Button buttonsubmit;

DatePickerDialog dpdfrom, dpdto;

Date dtFrom, dtTo;

long milisto, milisfrom, days, diff;

int i;

Button delete;
TableLayout stk;
TextView tvadd, tvadd2, tvadd3, tvfrompick, tvtopick, t1v;

String abc;
DatefromTo datefromto, dtFT;

int listSize;

int mYearfrom, mMonthfrom, mDayfrom, mYearto, mMonthto, mDayto;

static List<DateClassObject> myList;

Calendar from; // get the from date from user and store here
Calendar to; // get the to date from user and store here

EditText et;
String datetoday, difference, string1, string2;

Long bringto, bringfrom, between;
boolean wrong;
boolean entryremover;

// boolean yrfm , mnfm , dyfm , yrto , mnto , dyto;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dateadd);

from = Calendar.getInstance();

to= Calendar.getInstance();

final Calendar c = Calendar.getInstance();
mYearfrom = c.get(Calendar.YEAR);
mMonthfrom = c.get(Calendar.MONTH);
mDayfrom = c.get(Calendar.DAY_OF_MONTH);

mYearto = c.get(Calendar.YEAR);
mMonthto = c.get(Calendar.MONTH);
mDayto = c.get(Calendar.DAY_OF_MONTH);

buttonsubmit = (Button) findViewById(R.id.buttonsubmit);

et = (EditText) findViewById(R.id.et);

// tvspecial = (TextView) findViewById(R.id.tvspecial);

tvfrompick = (TextView) findViewById(R.id.tvfrompick);
tvtopick = (TextView) findViewById(R.id.tvtopick);

myList = new ArrayList<DateClassObject>();

i = (myList.size()-1);

init();

if (entryremover == true) {

delete.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
myList.remove(dtFT);

}
});
}

buttonsubmit.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

dtFT = new DatefromTo(from,to);





if(from.before(to)){

myList.add(dtFT);

AddEntry();
}

for(int j = 0; j < myList.size(); j ++){ //Note. you've written,
//for(int j = 1; j <= myList.size(); j ++) , ArrayList index start from 0. you'll miss the first element. I hope it helps you in the future

Calendar dateFrom = myList.get(j).dateFrom;
Calendar dateTo = myList.get(j).dateTo;

if(isSameDay(from, dateFrom) && isSameDay(to, dateTo)){ // checking same period allready exists

Toast.makeText(getApplicationContext(),
"Already exist",
Toast.LENGTH_LONG).show();
}
else{
// if it does not exists, I assume that you want to add it your arrayList
myList.add(new DatefromTo(from, to));
}

t1v.setText((myList.get(j).dateFrom
+ "\n" + myList.get(j).dateTo + "\n"));

}


tvfrompick.setText("");

tvtopick.setText("");









}
});



tvfrompick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

dpdfrom.show();

}
});

tvtopick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

dpdto.show();

}
});

dpdfrom = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {

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

tvfrompick.setText(dayOfMonth + "-" + monthOfYear + 1
+ "-" + year);

mDayfrom = dayOfMonth;
mMonthfrom = (monthOfYear);
mYearfrom = year;

// dtFrom = new Date(mYearfrom - 1900, mMonthfrom,mDayfrom);


from.set(mYearfrom, mMonthfrom,
mDayfrom);
}
}, mYearfrom, mMonthfrom, mDayfrom);

dpdto = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {

@SuppressWarnings({ "deprecation", "deprecation" })
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {

tvtopick.setText(dayOfMonth + "-" + monthOfYear + 1
+ "-" + year);

mDayto = dayOfMonth;
mMonthto = (monthOfYear);
mYearto = year;


// dtTo = new Date((mYearto - 1900), mMonthto, mDayto);
to.set( mYearto, mMonthto, mDayto);

}
}, mYearto, mMonthto, mDayto);


}

public boolean isSameDay(Calendar cal1, Calendar cal2) {
if (cal1 == null || cal2 == null) {
throw new IllegalArgumentException("null");
}
return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
}


public void init() {
stk = (TableLayout) findViewById(R.id.table_main);
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText("From");
tv0.setTextColor(Color.WHITE);
tv0.setPadding(20, 10, 20, 10);
tbrow0.addView(tv0);

TextView tv1 = new TextView(this);
tv1.setText(" To " + myList.size());
tv1.setTextColor(Color.WHITE);
tv1.setPadding(20, 10, 20, 10);
tv1.getPaddingLeft();
tbrow0.addView(tv1);
// Button tv2 = new Button(this);
// tv2.setText("Delete ");
// tv2.getPaddingLeft();
// //tv2.setTextColor(Color.WHITE);
// tbrow0.addView(tv2);

stk.addView(tbrow0);
}

public void AddEntry() {
entryremover = true;
TableRow tbrow = new TableRow(this);
t1v = new TextView(this);
// t1v.setText("From" + i);
myList.get(myList.size() - 1);
t1v.setTextColor(Color.WHITE);
t1v.setGravity(Gravity.CENTER);
t1v.setPadding(20, 10, 20, 10);
tbrow.addView(t1v);

delete = new Button(this);
delete.setText("Delete");
delete.setTextColor(Color.WHITE);
delete.setGravity(Gravity.CENTER);
delete.setPadding(20, 10, 20, 10);

tbrow.addView(delete);

stk.addView(tbrow);
}

public void removeEntry() {

}
}

最佳答案

使用日历而不是日期。 (对我来说更容易,因为我在我正在从事的项目中拥有你正在寻找的大部分内容)

List<DateClassObject> myList = new ArrayList<>();

Calendar from = // get the from date from user and store here
Calendar to = // get the to date from user and store here

//to check 'from' is after 'to'

if(from.after(to)){
// Do your operation
}

for(int j = 0; j < myList.size(); j ++){ //Note. you've written, for(int j = 1; j <= myList.size(); j ++) , ArrayList index start from 0. you'll miss the first element. I hope it helps you in the future

Calendar dateFrom = myList.get(j).dateFrom;
Calendar dateTo = myList.get(j).dateTo;

if(isSameDay(from, dateFrom) && isSameDay(to, dateTo)){ // checking same period allready exists
// if it exists Do operation
}
else{
// if it does not exists, I assume, you want to add DateClassObject to your arrayList
myList.add(new DateClassObject(from, to));
}
}

public boolean isSameDay(Calendar cal1, Calendar cal2) {
if (cal1 == null || cal2 == null) {
throw new IllegalArgumentException("The dates must not be null");
}
return (cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) &&
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH));
}

public class DateClassObject {

public Calendar dateFrom;
public Calendar dateTo;

public Date difference;

public DateClassObject(Calendar dateFrom, Calendar dateTo) {
this.dateFrom = dateFrom;
this.dateTo = dateTo;
}
}

我想这就是您要找的。问候

编辑:

像这样将日期选择器中选择的日期存储到日历实例中。

int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth();
int year = datePicker.getYear();

Calendar from = Calendar.getInstance();
from.set(year, month, day);

编辑:

好的,我对您的按钮点击做了一些更改,在 if 语句中添加 for 循环。如果我假设正确,那么只有当 fromDate 不在 toDate 之前并且它不是重复条目时,你才想添加到 arraylist

buttonsubmit.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

if(from.before(to)){

boolean alreadyExists = false;
for(int j = 0; j < myList.size(); j ++){

Calendar dateFrom = myList.get(j).dateFrom;
Calendar dateTo = myList.get(j).dateTo;

if(isSameDay(from, dateFrom) && isSameDay(to, dateTo)){ // checking same period allready exists
alreadyExists = true;
break;
}
}
if (alreadyExists){
Toast.makeText(getApplicationContext(),
"Already exist",
Toast.LENGTH_LONG).show();
}
else{
myList.add(new DateClassObject(from, to));
}
}
tvfrompick.setText("");
tvtopick.setText("");
}
});

至于您在代码 myList.remove(dtFT); 中编写的删除按钮。 remove 方法将数组列表中的位置作为参数(整数值)。myList.remove(2) 从列表中删除第二个元素。所以找出一些方法来获取要删除的元素的位置。

关于java - 如何比较两个java sql日期,一个在列表中,另一个刚刚输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30587905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com