时间戳工具类

package com.common;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.TimeZone;

public class Common {

    /**
     * 获取当前毫秒转int
     * 
     * @return
     */
    public Integer getCurTimestamp() {
        long l = System.currentTimeMillis();
        return (int) (l / 1000);
    }

    // 获取今天0点的时间戳
    public static Integer getCurDayZeroTimestamp(){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (int) (cal.getTimeInMillis()/1000);
    }

    // 获取 当月0点的时间戳
    public static Integer getCurMonthZeroTimestamp(){
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
        cal.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return (int) (cal.getTimeInMillis()/1000);
    }

    // 获取今年 0点的时间戳
    public static Integer getCurYearZeroTimestamp(){

        Calendar c = Calendar.getInstance();  
        c.add(Calendar.YEAR, 0);  
        c.set(Calendar.DAY_OF_YEAR, 1);//设置为1号,当前日期既为本年第一天  
        //将小时至0  
        c.set(Calendar.HOUR_OF_DAY, 0);  
        //将分钟至0  
        c.set(Calendar.MINUTE, 0);  
        //将秒至0  
        c.set(Calendar.SECOND,0);  
        //将毫秒至0  
        c.set(Calendar.MILLISECOND, 0);  
        // 获取今天第一天的时间戳  
        return (int) (c.getTimeInMillis()/1000);
    }

    /* 
     * 将时间转换为时间戳
     */    
    public static String dateToStamp(String s) throws ParseException{
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime()/ 1000;
        res = String.valueOf(ts);
        return res;
    }

    /* 
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt*1000);
        res = simpleDateFormat.format(date);
        return res;
    }
    /**
     * 计算当前月 , 共有多少天
     * 
     * @return
     */
    private int getDaysByYearMonth() {
        Calendar cal = Calendar.getInstance(); // 调用Calendar 中的方法;
        cal.set(Calendar.DAY_OF_MONTH, 1); // 把时间调整为当月的第一天;
        cal.add(Calendar.MONTH, 1); // 月份调至下个月;
        cal.add(Calendar.DAY_OF_MONTH, -1); // 时间减去一天(就等于上个月的最后一天)
        int month = cal.get(Calendar.MONTH) + 1; // 调取月份(月份在表示中会少 1,如:1月份得出数字是
                                                    // 0;
        int days = cal.get(Calendar.DAY_OF_MONTH);// 调取当月的天数。
        return days;
    }

}
<think>好的,我现在需要回答用户关于Java时间戳生成工具类的示例代码的问题。首先,用户希望得到一个工具类,能够生成时间戳,可能还包含一些常见操作。根据用户提供的引用,特别是引用[1]和引用[2],用户提到了截取时间戳后六位以及使用时间戳作为随机数种子的例子。此外,引用[3]中的Snowflake算法可能相关,但用户当前的问题更基础,可能暂时不需要涉及分布式ID生成。 首先,我需要确定用户的具体需求。用户想要一个工具类,可能包括获取当前时间戳、格式化时间戳、转换为不同单位(如秒、毫秒)、以及可能的截取部分数字。参考引用[1],截取后六位是通过将时间戳转为字符串后取子串实现的。而引用[2]展示了如何用时间戳作为随机数种子,这可能属于工具类的扩展功能。 接下来,我需要设计一个工具类,包含常用的方法。比如获取当前毫秒级时间戳、秒级时间戳,格式化为字符串,解析字符串为时间戳,以及截取后N位。同时,可能需要考虑线程安全和易用性,所以工具类通常设计为不可实例化,使用静态方法。 然后,编写示例代码。需要确保代码正确,比如使用System.currentTimeMillis()获取时间戳,使用SimpleDateFormat进行格式化时注意线程安全问题。对于截取后N位,需要处理时间戳长度不足的情况,比如用户要后六位,但时间戳可能只有10位(秒级),这时候可能需要补零或者抛异常,根据需求决定。这里参考引用[1],直接截取后六位,如果不足则取全部。 另外,用户可能还需要将时间戳转换为日期对象,或者进行加减操作,这些可以作为工具类的方法加入。同时,要注意Java 8以上的时间API,比如Instant、LocalDateTime,但用户可能还在使用旧版本,所以使用传统的Date和SimpleDateFormat。 最后,生成相关问题,需要围绕时间戳处理、工具类设计、性能优化、应用场景等方面提问,帮助用户深入理解。 检查是否有遗漏,比如线程安全的问题,SimpleDateFormat不是线程安全的,所以在工具类中使用时需要每次创建新实例,或者使用ThreadLocal。这里为了示例简单,可能直接创建新实例,但实际应用中需要注意。 总结,工具类应包含以下方法: 1. 获取当前毫秒时间戳 2. 获取当前秒时间戳 3. 格式化时间戳为字符串 4. 解析字符串为时间戳 5. 截取时间戳后N位 6. 转换为Date对象 现在编写代码,并确保符合用户的引用内容和需求。</think>以下是一个包含常用操作的Java时间戳工具类实现,支持毫秒/秒级时间戳获取、格式化输出和字符串解析: ```java import java.text.SimpleDateFormat; import java.util.Date; public class TimestampUtils { // 禁止实例化 private TimestampUtils() {} /** * 获取当前毫秒级时间戳 */ public static long getCurrentMillis() { return System.currentTimeMillis(); } /** * 获取当前秒级时间戳 */ public static long getCurrentSeconds() { return System.currentTimeMillis() / 1000; } /** * 格式化时间戳(默认格式:yyyy-MM-dd HH:mm:ss) * @param timestamp 毫秒级时间戳 */ public static String format(long timestamp) { return format(timestamp, "yyyy-MM-dd HH:mm:ss"); } /** * 自定义格式时间戳 * @param timestamp 毫秒级时间戳 * @param pattern 日期格式 */ public static String format(long timestamp, String pattern) { SimpleDateFormat sdf = new SimpleDateFormat(pattern); return sdf.format(new Date(timestamp)); } /** * 将日期字符串解析为时间戳 * @param dateStr 日期字符串 * @param pattern 对应日期格式 */ public static long parse(String dateStr, String pattern) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat(pattern); return sdf.parse(dateStr).getTime(); } /** * 截取时间戳后N位数字 * @param timestamp 时间戳 * @param length 需要截取的长度 */ public static String getLastDigits(long timestamp, int length) { String str = String.valueOf(timestamp); return str.substring(Math.max(str.length() - length, 0)); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值