日期计算

    xiaoxiao2021-03-25  115

    SimpleDateFormat  这个是对于时间好用

    SimpleDateFormate(String pattern)

    String format(Date date)

    Date parse(String source)

    -------------------------------------------------------

    对于Date 类

    大部分都被不建议用了,但是有一个好用的方法是getTime() 比较好用

    -------------------------------------------------------------

    日期计算

    时间限制:3000 ms  |  内存限制:65535 KB 难度:1 描述     如题,输入一个日期,格式如:2010 10 24 ,判断这一天是这一年中的第几天。 输入     第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据, 每行的输入数据都是一个按题目要求格式输入的日期。 输出     每组输入数据的输出占一行,输出判断出的天数n297 样例输入     3     2000 4 5     2001 5 4     2010 10 24 样例输出     96     124     297 --------------------------------------------------------------- import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; class Test{     private String time;     private String temp;     public Test(String time) throws ParseException {         super();         this.time = time;         fun();     }          public void fun() throws ParseException{         Scanner scan=new Scanner(this.time);         StringBuffer sb=new StringBuffer();         while(scan.hasNext()){             int temp=scan.nextInt();             if(temp<10)                 sb.append("0"+temp+"-");             else                 sb.append(temp+"-");         }         sb.deleteCharAt(sb.length()-1);         this.time=sb.toString();         this.temp=sb.delete(4, sb.length()).toString()+"-01-00";     //    System.out.println(this.time);         //System.out.println(this.temp);         DateFormat df = new SimpleDateFormat("yyyy-MM-dd");                 Date d1 = df.parse(this.time);                 Date d2 = df.parse(this.temp);                  long diff = d1.getTime() - d2.getTime();         long days = diff / (1000 * 60 * 60 * 24);         System.out.println(days);     } } public class Timer {     public static void main(String[] args) throws ParseException {         Test t=new Test("2001 5 4");                  } }
    转载请注明原文地址: https://ju.6miu.com/read-14377.html

    最新回复(0)