比较公历日期值

发布于 2024-11-14 03:01:17 字数 2048 浏览 3 评论 0原文

我正在尝试设置一个程序的一部分,该程序允许人们根据交易日期查看帐户的交易。用户输入年月日来查看交易,并将其与连接到给定交易的日期进行比较。我很难编写确定日期是否相等的代码行

if(aBank.getAccounts().get(i).getTransaction().get(j).getTransDate().get(Calendar.MONTH).compareTo(month)==0){
                        if(aBank.getAccounts().get(i).getTransaction().get(j).getTransDate().get(Calendar.DAY_OF_MONTH).compareTo(day)==0){
                            if(aBank.getAccounts().get(i).getTransaction().get(j).getTransDate().get(Calendar.YEAR).compareTo(year)==0){

我收到的错误是“无法在基本类型 int 上调用compareTo(int)” 请参阅下面的完整代码:

System.out.println("Enter the account number of the account that you want to view transactions for");
            number=keyboard.nextLong();
            System.out.println("Enter the month day and year of the date that the transactions were completed");
            int month=keyboard.nextInt()-1;
            int day=keyboard.nextInt();
            int year=keyboard.nextInt();
            found=false;
            try{
            for(int i=0;i<aBank.getAccounts().size();i++){
                if (aBank.getAccounts().get(i).getAccountNumber().compareTo(number)==0){
                    found=true;
                    System.out.println("Below is a list of transactions completed on "+month+ "/" +day+ "/" +year);
                    for (int j=0;j<aBank.getAccounts().get(i).getTransaction().size();j++){
                    if(aBank.getAccounts().get(i).getTransaction().get(j).getTransDate().get(Calendar.MONTH).compareTo(month)==0){
                        if(aBank.getAccounts().get(i).getTransaction().get(j).getTransDate().get(Calendar.DAY_OF_MONTH).compareTo(day)==0){
                            if(aBank.getAccounts().get(i).getTransaction().get(j).getTransDate().get(Calendar.YEAR).compareTo(year)==0){
                                aBank.getAccounts().get(i).getTransaction().get(j).toString();
                                break;
                            }
                        }

                    }

                }

I am trying to set up part of a program that allows a person to view transactions of an account based on the date of the transaction. The user enters the month day and year to view transactions and that is compared to the date that is connected to a given transaction. I am having difficult writing the lines of code that determine if the date is equal

if(aBank.getAccounts().get(i).getTransaction().get(j).getTransDate().get(Calendar.MONTH).compareTo(month)==0){
                        if(aBank.getAccounts().get(i).getTransaction().get(j).getTransDate().get(Calendar.DAY_OF_MONTH).compareTo(day)==0){
                            if(aBank.getAccounts().get(i).getTransaction().get(j).getTransDate().get(Calendar.YEAR).compareTo(year)==0){

The error that I am receiving is "cannot invoke compareTo(int) on the primitive type int"
see full code below:

System.out.println("Enter the account number of the account that you want to view transactions for");
            number=keyboard.nextLong();
            System.out.println("Enter the month day and year of the date that the transactions were completed");
            int month=keyboard.nextInt()-1;
            int day=keyboard.nextInt();
            int year=keyboard.nextInt();
            found=false;
            try{
            for(int i=0;i<aBank.getAccounts().size();i++){
                if (aBank.getAccounts().get(i).getAccountNumber().compareTo(number)==0){
                    found=true;
                    System.out.println("Below is a list of transactions completed on "+month+ "/" +day+ "/" +year);
                    for (int j=0;j<aBank.getAccounts().get(i).getTransaction().size();j++){
                    if(aBank.getAccounts().get(i).getTransaction().get(j).getTransDate().get(Calendar.MONTH).compareTo(month)==0){
                        if(aBank.getAccounts().get(i).getTransaction().get(j).getTransDate().get(Calendar.DAY_OF_MONTH).compareTo(day)==0){
                            if(aBank.getAccounts().get(i).getTransaction().get(j).getTransDate().get(Calendar.YEAR).compareTo(year)==0){
                                aBank.getAccounts().get(i).getTransaction().get(j).toString();
                                break;
                            }
                        }

                    }

                }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌若浮生 2024-11-21 03:01:17

对于原始值,您可以使用 ==

aBank.getAccounts().get(i).getTransaction().get(j).getTransDate().get(Calendar.YEAR)==year

For primitive values you can just use ==

aBank.getAccounts().get(i).getTransaction().get(j).getTransDate().get(Calendar.YEAR)==year
青萝楚歌 2024-11-21 03:01:17

只需使用:

aBank.getAccounts().get(i).getTransaction().get(j).getTransDate().get(Calendar.MONTH) == Month

Just use:

aBank.getAccounts().get(i).getTransaction().get(j).getTransDate().get(Calendar.MONTH) == month

寻找一个思念的角度 2024-11-21 03:01:17

如果所有 XYZ.getTransDate() 返回 Calendar,则
XYZ.getTransDate().get(SOMETHING) 返回原始 int。基元没有 comapreTo 方法,只需使用 ==

因此,不要使用 XYZ.getTransDate().get(MONTH).compareTo(month) == 0 使用
XYZ.getTransDate().get(MONTH) == 月份

If all of the XYZ.getTransDate() returns Calendar, then
XYZ.getTransDate().get(SOMETHING) returns primitive int. Primitives do not have comapreTo method, just use ==

so instead of XYZ.getTransDate().get(MONTH).compareTo(month) == 0 use
XYZ.getTransDate().get(MONTH) == month

花辞树 2024-11-21 03:01:17

这应该有效:

Calendar transDate = aBank.getAccounts().get(i).getTransaction().get(j).getTransDate();
if (transDate.get(Calendar.YEAR) == year &&
    transDate.get(Calendar.MONTH) == month &&
    transDate.get(Calendar.DAY_OF_MONTH) == day) {

    // do something
}

如果您使用类似 Apache Commons Lang 的东西,那就更好了:

if (DateUtils.isSameDay(aBank.getAccounts().get(i).getTransaction().get(j).getTransDate(),
                        Calendar.getInstance().set(year, month, day)) {
    ...
}

This should work:

Calendar transDate = aBank.getAccounts().get(i).getTransaction().get(j).getTransDate();
if (transDate.get(Calendar.YEAR) == year &&
    transDate.get(Calendar.MONTH) == month &&
    transDate.get(Calendar.DAY_OF_MONTH) == day) {

    // do something
}

Even better if you use something like Apache Commons Lang:

if (DateUtils.isSameDay(aBank.getAccounts().get(i).getTransaction().get(j).getTransDate(),
                        Calendar.getInstance().set(year, month, day)) {
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文