时间与时间戳的转换

/** 时间小知识:
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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: JavaScript 提供了一些内置的函数和对象可以方便地实现时间时间戳的相互转换。 要将时间戳转换时间,可以使用 JavaScript 的 Date 对象。例如: ``` // 将时间戳转换时间 var timestamp = 1615574400; var date = new Date(timestamp * 1000); // 使用 Date 对象的方法格式化时间 console.log(date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate()); // 2021-3-31 ``` 要将时间转换时间戳,可以使用 Date 对象的 `getTime()` 方法。例如: ``` // 将时间转换时间戳 var date = new Date("2022-12-17T00:00:00"); var timestamp = Math.floor(date.getTime() / 1000); console.log(timestamp); // 1675446400 ``` 注意,JavaScript 的时间戳是以毫秒为单位的,如果要将其转换为以秒为单位的时间戳,需要使用数学函数 `Math.floor()` 对时间戳进行取整。 另外,还可以使用 JavaScript 的 `toISOString()` 方法将时间转换为 ISO 格式的字符串,再使用正则表达式截取其中的时间戳。例如: ``` // 将时间转换为 ISO 格式的字符串 var date = new Date("2022-12-17T00:00:00"); var isoString = date.toISOString(); // 使用正则表达式截取时间戳 var timestamp = isoString.match(/\d{10}/g)[0]; console.log(timestamp); // 1675446400 ``` 希望这些信息能帮到你! ### 回答2: JavaScript中可以使用Date对象进行时间时间戳之间的转换。 1. 将时间转换时间戳: 要将一个特定的时间转换时间戳,可以使用Date对象的getTime()方法。该方法返回的是一个自1970年1月1日以来经过的毫秒数。例如,可以使用下面的代码将当前时间转换时间戳: ``` var timestamp = new Date().getTime(); ``` 2. 将时间戳转换时间: 要将一个时间戳转换时间,可以使用Date对象的构造函数,将时间戳作为参数传入。然后,可以使用该对象的各种方法(例如getFullYear()、getMonth()、getDate()等)来获取年、月、日、时、分、秒等信息。例如,可以使用下面的代码将一个时间戳转换时间: ``` var timestamp = 1609459200000; // 2021年1月1日的时间戳 var date = new Date(timestamp); console.log(date); ``` 以上代码将打印出:Fri Jan 01 2021 08:00:00 GMT+0800 (中国标准时间) 需要注意的是,JavaScript中的时间戳是以毫秒为单位的,而不是以秒为单位的。所以在进行时间戳转换时,需要确保使用正确的单位。 总结起来,通过Date对象的getTime()方法可以将时间转换时间戳,通过Date对象的构造函数可以将时间戳转换时间。 ### 回答3: JavaScript中时间时间戳之间的相互转换非常简单。时间戳是指从1970年1月1日00:00:00格林威治标准时间(UTC)开始经过的毫秒数。而JavaScript的Date对象表示一个特定的日期时间。 要将时间戳转换为JavaScript的Date对象,可以使用Date的构造函数。例如,将时间戳1234567890转换为Date对象的代码如下: ```javascript var timestamp = 1234567890; var date = new Date(timestamp); ``` 这样就可以得到表示时间戳对应日期时间的Date对象。 相反,要将JavaScript的Date对象转换时间戳,可以使用Date对象的getTime()方法。该方法返回自1970年1月1日00:00:00 UTC到指定日期时间之间经过的毫秒数。例如,将Date对象转换时间戳的代码如下: ```javascript var date = new Date(); var timestamp = date.getTime(); ``` 这样就可以得到表示指定Date对象对应的时间戳。 总之,要在JavaScript中进行时间时间戳之间的转换,可以使用Date对象的构造函数将时间戳转换为Date对象,或者使用Date对象的getTime()方法将Date对象转换时间戳。这两种方法对于时间时间戳之间的转换非常方便。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值