Java时间类型

举例:

获取年份后两位

//获取年份后两位
String year =new SimpleDateFormat("yy", Locale.CHINESE).format(new Date());

获取月份:要记得先new一个Date出来

 //获取月份两位,月份要加1
int month = qaDate.getMonth() + 1;

获取日期:要记得先new一个Date出来

int date = qaDate.getDate();

生成三位随机数,在大于100,小于900之间

int i = (int)(Math.random()*900)+100;

完整代码

        Date qaDate = new Date();
        //生成送货单号
        String qaNumber = "IN";
        //获取年份后两位
        String year =new SimpleDateFormat("yy", Locale.CHINESE).format(new Date());
        //获取月份两位,月份要加1
        int month = qaDate.getMonth() + 1;
        //获取日期两位,
        int date = qaDate.getDate();
        //生成三位流水号
        int i = (int)(Math.random()*900)+100;
        System.out.println(qaNumber+year+month+date+i);

实体类的时间类型:年月日+时分秒(yyyy-MM-dd HH:mm:ss )

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date qaDate;

实体类的时间类型:年月日

    @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date orderInfoTime;

获取日期

SimpleDateFormat sy=new SimpleDateFormat("yyyy");
SimpleDateFormat sm=new SimpleDateFormat("MM");
SimpleDateFormat sd=new SimpleDateFormat("dd");
String syear=sy.format(date);
String smon=sm.format(date);
String sday=sd.format(date);

 

 

欢迎关注作者微信公众号

 

posted @ 2021-10-21 11:56  沾青先生  阅读(588)  评论(0编辑  收藏  举报