时间与时间戳的转换

/** 时间小知识:
y年M月d日 H时m分s秒,这是字母分别代表的时间,
在转换时,如果只填一个字母,则即使那个时间只有一个数字也不会有多余的0。
比如用y年M月d日 H时m分s秒 来转换时间戳,转换的时间是2017年3月5天 3时8分4秒 
比如用yyyy年MM月dd日 HH时mm分ss秒 来转换时间戳,转换的时间是2017年03月05天 03时08分04秒*/

//将十位数时间戳转换为时间日期
public String times(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("y年M月d日 H时m分s秒");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;

    }

// 毫秒数转成给定格式的时间
    public static String longToDate(String format, long time) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        Date curDate = new Date(time);// 获取当前时间
        return formatter.format(curDate);
    }

    /** 时间戳转换成指定时间格式 */
    public static String getFormatDate(long time, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        Date curDate = new Date(time);// 获取当前时间
        return formatter.format(curDate);
    }

// 毫秒数转成星期
    public static String longToWeek(long time) {
        String week = "星期";
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            week += "天";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 2) {
            week += "一";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 3) {
            week += "二";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 4) {
            week += "三";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 5) {
            week += "四";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 6) {
            week += "五";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 7) {
            week += "六";
        }
        return week;
    }

/**
     * 得到本周周一
     * 
     * @return yyyy-MM-dd
     */
    public static String getMondayOfThisWeek() {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        c.add(Calendar.DATE, -day_of_week + 1);
        return dateFormat.format(c.getTime());
    }

    /**
     * 得到本周周日
     * 
     * @return yyyy-MM-dd
     */
    public static String getSundayOfThisWeek() {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        c.add(Calendar.DATE, -day_of_week + 7);
        return dateFormat.format(c.getTime());
    }

/**
     * 获取当月的 天数
     * 
     * @return
     */
    public static int getCurrentMonthDays() {

        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

/**
     * 获取当月最后一天的时间
     * 
     * @return
     */
    public static String getLastDayOfThisMonth() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH,
                ca.getActualMaximum(Calendar.DAY_OF_MONTH));

        String lastDay = format.format(ca.getTime());
        return lastDay;
    }
/**
     * 获取当月第一天的时间
     * 
     * @return
     */
    public static String getFirDayOfThisMonth() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天

        String firstDay = format.format(c.getTime());
        return firstDay;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值