java date与日期转换操作

79 阅读1分钟

date 已经废弃使用

package imooc.chap01.vl.date;

import java.util.Date;

public class test1 {
    public static void main(String[] args) throws InterruptedException {
        Date currentTime = new Date();
        Thread.sleep(3000);
        Date newTime =new Date();
        long msDelay=newTime.getTime()- currentTime.getTime();
        System.out.println(msDelay);
    }
}
package imooc.chap01.vl.date;

import java.util.Date;

public class test2 {
    public static void main(String[] args) throws InterruptedException {
        Date startTime = new Date();
        long endTime=startTime.getTime()+5000;
        Date enddate = new Date(endTime);
        Thread.sleep(3000);

        Date currentTime = new Date();
        //检查 currentTime 是否在 endDate 之后
        if (currentTime.after(enddate)){
            System.out.println("结束时间!");
        }
    }
}
package imooc.chap01.vl.date;

import java.util.Date;

public class test3 {
    public static void main(String[] args) {
        Date currentTime = new Date();
        int hours = currentTime.getHours();
        int minutes = currentTime.getMinutes();
        int seconds = currentTime.getSeconds();
        System.out.println(hours+":"+minutes+":"+seconds);



    }
}
package imooc.chap01.vl.date;

import java.time.Year;
import java.util.Date;

public class test4 {
    public static void main(String[] args) throws Exception{
        Date yearStartTime = new Date();
        yearStartTime.setHours(0);
        yearStartTime.setMinutes(0);
        yearStartTime.setSeconds(0);
        yearStartTime.setDate(1);
        yearStartTime.setMonth(0);
        Date currentTime=new Date();
        long msTimeDifference=currentTime.getTime()- yearStartTime.getTime();
        long msDay=24*60*60*1000;
        int dayCount=(int) (msTimeDifference/msDay);
        System.out.println(dayCount);
    }
}

日期转换Locale.ENGLISH表示用英文显示,MMM几个表示显示几位

package zh.codegym.task.task09.task0922;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/* 
今天是几号?
*/

public class Solution {

    public static void main(String[] args) throws Exception {
        //在此编写你的代码
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd",Locale.ENGLISH);
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("MMM dd, yyyy",Locale.ENGLISH);
        Date parse = simpleDateFormat.parse(reader.readLine());
        System.out.println(simpleDateFormat1.format(parse).toUpperCase(Locale.ROOT));


    }
}