package com.api.dateandtime;
//Date类
import java.util.Date;

public class DateDemo01 {
    public static void main(String[] args) {
        //1、创建一个Date类的对象:代表系统此刻日期时间对象
        Date d = new Date();
        System.out.println(d.toString());
        //2、获取时间毫秒值
        long time = d.getTime();
        System.out.println(time);
        //long time1 = System.currentTimeMillis();
        //System.out.println(time1);
        System.out.println("=======================================================");
        //计算出当前时间往后走1小时121秒之后的时间是多少。
        Date d2 = new Date();
        System.out.println(d2);
        long time2 = d2.getTime();
        long time3 = time2 + (60*60 + 121)*1000;
        //Date d3 = new Date(time3);
        //System.out.println(d3);
        Date d3 = new Date();
        d3.setTime(time3);
        System.out.println(d3);

    }
}

package com.api.dateandtime;
//class SimpleDateFormat 的 格式化日期
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatDemo01 {
    public static void main(String[] args) {
        //1、日期对象
        Date d = new Date();
        System.out.println(d);

        //2、格式化这个日期对象〔指定最终格式化的形式)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a");

        //开始格式化日期对象成为喜欢的字符串形式
        String s1 = sdf.format(d);
        System.out.println(s1);
        System.out.println("======================================");

        //4、格式化时间毫秒值
        //需求:请问121秒后的时间是多少
        long time = System.currentTimeMillis();

        String s2 =sdf.format(time);
        System.out.println(s2);

        time += 121*1000;
        String s3 =sdf.format(time);
        System.out.println(s3);
    }
}
package com.api.dateandtime;
//class SimpleDateFormat的parse()方法
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatDemo02 {
    public static void main(String[] args) throws ParseException {
        String s = "2021年8月6日 11:11:11";

        //把字符串时间解析成日期对象(本节的重点):形式必须与被解析时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date d = sdf.parse(s);

        long t = d.getTime();
        t += (2L*24*60*60+14*60*60+49*60+6)*1000;

        String s1 = sdf.format(t);
        System.out.println(s1);
    }
}

package com.api.dateandtime;
//SimpleDateFormat秒杀案例
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatTest {
    public static void main(String[] args) throws ParseException {
        String st = "2021-11-11 00:00:00";
        String et = "2021-11-11 00:10:00";

        String xj = "2021-11-11 00:03:47";
        String xp = "2021-11-11 00:10:11";

        //解析时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1 = sdf.parse(st);
        Date d2 = sdf.parse(et);
        Date d3 = sdf.parse(xj);
        Date d4 = sdf.parse(xp);


        if (d3.after(d1) && d3.before(d2)){
            System.out.println("xj秒杀成功!");
        }else {
            System.out.println("xj秒杀失败!");
        }
        if (d4.after(d1) && d4.before(d2)){
            System.out.println("xp秒杀成功!");
        }else {
            System.out.println("xp秒杀失败!");
        }
    }
}
package com.api.dateandtime;
//Calendar
import java.util.Calendar;
import java.util.Date;

public class CalendarDemo01 {
    public static void main(String[] args) {
        // 1、拿到系统此刻日历对象
        Calendar c = Calendar.getInstance();
        System.out.println(c);

        //获取日历的信息: public int get(int field):取日期中的某个字段信息。
        System.out.println(c.get(Calendar.YEAR));
        System.out.println(c.get(Calendar.MONTH) + 1);
        System.out.println(c.get(Calendar.DAY_OF_WEEK));

        //3、public void set(int field,int value):修改日历的某个字段信息。
        c.set(Calendar.HOUR,7);
        System.out.println(c);

        //public void add(int field,int amount):为某个字段增加/减少指定的值
        //请问64天后是什么时间
        c.add(Calendar.DAY_OF_YEAR,64);

        //public final Date getTime():拿到此刻日期对象。
        Date d = c.getTime();
        System.out.println(d);

        //public long getTimeInMillis():拿到此刻时间毫秒值
        long time = c.getTimeInMillis();
        System.out.println(time);
    }
}

原文地址:http://www.cnblogs.com/799rijiyuelei/p/16905000.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性