以下是一个Java 6时间工具类的示例代码,它包含了常见的时间操作和格式化方法:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class TimeUtils {

    // 时间格式
    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取当前时间戳(秒)
     */
    public static long getCurrentTimestamp() {
        return System.currentTimeMillis() / 1000;
    }

    /**
     * 获取指定时间戳的Date对象
     */
    public static Date getDateFromTimestamp(long timestamp) {
        return new Date(timestamp * 1000);
    }

    /**
     * 获取指定时间的时间戳(秒)
     */
    public static long getTimestampFromDate(Date date) {
        return date.getTime() / 1000;
    }

    /**
     * 获取指定时间的时间戳(秒),带时区
     */
    public static long getTimestampFromDate(Date date, String timeZone) {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(timeZone));
        calendar.setTime(date);
        return calendar.getTimeInMillis() / 1000;
    }

    /**
     * 获取指定时间的时间戳(秒),带时区
     */
    public static long getTimestampFromDate(String dateStr, String timeZone) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
        Date date = dateFormat.parse(dateStr);
        return date.getTime() / 1000;
    }

    /**
     * 获取当前时间的Date对象
     */
    public static Date getCurrentDate() {
        return new Date();
    }

    /**
     * 获取当前时间的时间戳(秒),带时区
     */
    public static long getCurrentTimestamp(String timeZone) {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(timeZone));
        return calendar.getTimeInMillis() / 1000;
    }

    /**
     * 获取当前时间的时间戳(秒),带时区
     */
    public static long getCurrentTimestampWithTimeZone(String timeZone) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
        Date date = dateFormat.parse(dateFormat.format(new Date()));
        return date.getTime() / 1000;
    }

    /**
     * 格式化时间为字符串
     */
    public static String format(Date date) {
        DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        return dateFormat.format(date);
    }

    /**
     * 格式化时间为字符串,带时区
     */
    public static String format(Date date, String timeZone) {
        DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
        return dateFormat.format(date);
    }

    /**
     * 格式化时间戳为字符串
     */
    public static String format(long timestamp) {
        Date date = getDateFromTimestamp(timestamp);
        return format(date);
    }

    /**
     * 格式化时间戳为字符串,带时区
     */
    public static String format(long timestamp, String timeZone) {
        Date date = getDateFromTimestamp(timestamp);
        return format(date, timeZone);
    }

    /**
     * 解析时间字符串为Date对象
     */
    public static Date parse(String dateStr) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        return dateFormat.parse(dateStr);
    }

    /**
     * 解析时间字符串为Date对象,带时区
     */
    public static Date parse(String dateStr, String timeZone) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
        return dateFormat.parse(dateStr);
    }

    /**
     * 解析时间字符串为时间戳(秒)
     */
    public static long parseTimestamp(String dateStr) throws ParseException {
        Date date = parse(dateStr);
        return getTimestampFromDate(date);
    }

    /**
     * 解析时间字符串为时间戳(秒),带时区
     */
    public static long parseTimestamp(String dateStr, String timeZone) throws ParseException {
        Date date = parse(dateStr, timeZone);
        return getTimestampFromDate(date);
    }
}

使用示例:

// 获取当前时间戳
long currentTimestamp = TimeUtils.getCurrentTimestamp();

// 获取当前时间的Date对象
Date currentDate = TimeUtils.getCurrentDate();

// 格式化时间为字符串
String formattedDate = TimeUtils.format(currentDate);

// 解析时间字符串为Date对象
Date parsedDate = TimeUtils.parse(formattedDate);

// 解析时间字符串为时间戳(秒)
long parsedTimestamp = TimeUtils.parseTimestamp(formattedDate);

// 获取当前时间的时间戳(秒),带时区
long currentTimestampWithTimeZone = TimeUtils.getCurrentTimestampWithTimeZone("Asia/Shanghai");

// 格式化时间为字符串,带时区
String formattedDateWithTimeZone = TimeUtils.format(currentDate, "Asia/Shanghai");

// 解析时间字符串为时间戳(秒),带时区
long parsedTimestampWithTimeZone = TimeUtils.parseTimestamp(formattedDateWithTimeZone, "Asia/Shanghai");
用java6写一个时间工具类非常多功能的那种

原文地址: https://www.cveoy.top/t/topic/bQE5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录