【问题标题】:Is there a library function that can give me the current date in variables and find the number of days passed since a given date [duplicate]是否有一个库函数可以给我变量中的当前日期并找到自给定日期以来经过的天数[重复]
【发布时间】:2017-05-14 11:58:48
【问题描述】:

我想获取当前日期和时间的变量

 int d,m,y,h,s;

到目前为止,我已经为 Date 创建了一个类,它可以让用户输入日期并检查日期的有效性,但我不知道如何获取自以来经过的天数以及当前日期。

我的上课日期 ->

import ShortHand.*;
public class Date {
    public static char[] digits={'0','1','2','3','4','5','6','7','8','9'};
    int d=0,m=0,y=0;
    public int getD() {
        return d;
    }
    public int getM() {
        return m;
    }
    public int getY() {
        return y;
    }
    public Date(int dd,int mm,int yy)
    {
        if (checkDate(dd,mm,yy)==false)
            throw new IllegalArgumentException("Invalid Date");
        this.d=dd;
        this.m=mm;
        this.y=yy;
    }   
    public static boolean checkDate(int d,int m,int y)
    {
        if (m<1||d<1||y<1)
            return false;
        if (m>12)
            return false;
        if (m==2&&checkLeap(y)&&d>29)
            return false;
        if (m==2&&!(checkLeap(y))&&m>28)
            return false;
        if ((m==1||m==3||m==5||m==7||m==8||m==10||m==12)&&d>31)
            return false;
        if ((m==4||m==6||m==8||m==11)&&d>30)
            return false;
        return true;
    }
    public static boolean checkLeap(int y)
    {
        if (y%100!=0)
        {
            if (y%4==0)
                return true;
        }
        else if (y%400==0)
            return true;
        return false;
    }
    public static Date takeIn(String msg)throws java.io.IOException
    {
        int d,m,y;
        while(true)
        {
            boolean f=true;
            u.pln(msg+"[dd-mm-yyyy]");
            String in=u.iBs();
            if (in.length()!=10)
            {
                u.pln("Please Enter a Valid Date");
                continue;
            }
            for (int i=0;i<10;i++)
            {
                if (i!=2&&i!=5)
                {
                    if (Arr.check(in.charAt(i), digits)==false)
                    {
                        u.pln("Invalid Input");
                        f=false;
                        break;
                    }
                }
            }
            if (f==false)
                continue;
            String t=in.substring(0,2);
            d=Integer.parseInt(t);
            t=in.substring(3,5);
            m=Integer.parseInt(t);
            t=in.substring(6);
            y=Integer.parseInt(t);
            if (checkDate(d,m,y))
                break;
        }
        return new Date(d,m,y);
    }
    @Override
    public String toString() {
        return this.d+"/"+this.m+"/"+this.y;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Date other = (Date) obj;
        if (d != other.d) {
            return false;
        }
        if (m != other.m) {
            return false;
        }
        if (y != other.y) {
            return false;
        }
        return true;
    }
}

【问题讨论】:

  • u.pln() 是我制作的一个函数,它将给定的字符串打印到控制台,并添加一个字符串,让我可以在执行结束时将整个输出写入文件跨度>

标签: java date calendar


【解决方案1】:

Java 8 中有 LocalDate 类,它会起到作用(以及更多):

LocalDate start = LocalDate.of(startYear, startMonth, startDay);
LocalDate end = LocalDate.of(endYear, endMonth, endDay);
long diff = start.until(end, ChronoUnit.DAYS);

要将当前日期分配给end,请使用now() 方法:

 LocalDate end = LocalDate.now();

【讨论】:

  • Vyskovsky kvr000 如何使用此类(或任何其他)获取当前日期
  • 您可以使用以下代码以任何格式获取当前日期 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");日期日期 = 新日期(); System.out.println(dateFormat.format(date));
  • @Pradnyarani 这是旧 API,与此答案中建议的完全不同。
  • @AkhandYaduvanshi 获取当前日期:LocalDate.now() - 如果可以选择,请不要使用 Date / SimpleDateFormat 类。
  • @Pradnyarani:您会考虑给出自己的答案,而不是向 cmets 添加代码吗?
【解决方案2】:

要获取当前日期,您可以使用 Java 8 Date Time API

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd"); 
LocalDate localDate = LocalDate.now(); 
System.out.println(dtf.format(localDate));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 2022-01-19
    相关资源
    最近更新 更多