时间

时间的格式详情: y 年      M 月     d 日    h 时 在上午或下午 (1~12)      H 时 在一天中 (0~23)    m 分   s 秒    S 毫秒    E 星期 

 D 一年中的第几天       F 一月中第几个星期几     w 一年中第几个星期      W 一月中第几个星期
 

SimpleDateFormat继承关系:

java.lang.Object
   |
   +----java.text.Format
           |
           +----java.text.DateFormat
                   |
                   +----java.text.SimpleDateFormat

设置需要输出的时间类型:写法可以不同 format()

Date date = new Date();

 SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");

SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm:ss"); 

SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 等同于 Date.toLocaleString()方法;

SimpleDateFormat myFmt4=new SimpleDateFormat("一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区");

 

时间戳转换为时间类型:

    public static String getDate(String s){
        SimpleDateFormat x = new SimpleDateFormat("yyyy-mm-dd");
        long a = new Long(s);
        Date date=new Date(a);
        String time = x.format(date);
        System.out.println(time);
        return time;
    }

时间转换为时间戳类型:

    public static String getDate2(String date) throws ParseException{
        SimpleDateFormat time =new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
        Date d =time.parse(date);
        String times =String.valueOf(d.getTime());
        return times;
    }

获取传入年,月的最后一天:

    public static String getlastdayofmonth(int year,int month){
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最大天数
        int lastDay=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        System.out.println(lastDay);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        return lastDayOfMonth;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值