date和时间字符串的转换

 1,获取系统时间方法一

String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
System.out.println(time);

// 注:把time转成date
 Date date= sdf.parse(time );

// 另外HH代表24小时,hh12小时

 2,获取系统时间方法二

String string ="yyyy-MM-dd HH:mm:ss";
String time = new SimpleDateFormat(string).format(System.currentTimeMillis());
System.out.println(time);

  3,获取系统时间方法三

public class TestDate {
    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        Date time = c.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(time);
        System.out.println(format);// 2018-08-02 15:07:19
    }
}
  • 获取前一年时间
 public class DateCalcTest{
    public static void main(String[] args)
    {
        Calendar calendar = Calendar.getInstance();
        Date date = new Date(System.currentTimeMillis());
        calendar.setTime(date);
        // calendar.add(Calendar.WEEK_OF_YEAR, -1);
        calendar.add(Calendar.YEAR, -1);
        date = calendar.getTime();
        System.out.println(date);
    }
} 
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
         
        //过去七天
        c.setTime(new Date());
        c.add(Calendar.DATE, - 7);
        Date d = c.getTime();
        String day = format.format(d);
        System.out.println("过去七天:"+day);
         
        //过去一月
        c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        Date m = c.getTime();
        String mon = format.format(m);
        System.out.println("过去一个月:"+mon);
         
        //过去三个月
        c.setTime(new Date());
        c.add(Calendar.MONTH, -3);
        Date m3 = c.getTime();
        String mon3 = format.format(m3);
        System.out.println("过去三个月:"+mon3);
         
        //过去一年
        c.setTime(new Date());
        c.add(Calendar.YEAR, -1);
        Date y = c.getTime();
        String year = format.format(y);
        System.out.println("过去一年:"+year);
  • 通过日期对象计算天数
private static void countDay() {
        try {
            long time1 = new Date().getTime();
            long time2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2017-08-02 09:58:11").getTime();
            int  days = (int) ((time1 - time2) / (1000*3600*24));
            System.out.println(days);// 365
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
  • 2个date或者字符串计算天数案例
package com.neo.controller;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TestDate {
    /**
     * @param args
     * @throws ParseException
     */
    public static void main(String[] args) throws ParseException {
        // TODO Auto-generated method stub
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1=sdf.parse("2012-09-08 10:10:10");
        Date d2=sdf.parse("2012-09-15 00:00:00");
        System.out.println(daysBetween(d1,d2));

        System.out.println(daysBetween("2012-09-08 10:10:10","2012-09-15 00:00:00"));
    }

    /**
     * 计算两个日期之间相差的天数 
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate,Date bdate) throws ParseException
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        smdate=sdf.parse(sdf.format(smdate));
        bdate=sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     *字符串的日期格式的计算
     */
    public static int daysBetween(String smdate,String bdate) throws ParseException{
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_39478044/article/details/81359370