时间戳,字符串转换小工具

  今天在堆代码的时候想实现将数据库的数据通过周报,月报的形式在页面上通过图标显示。发现数据库中存储的是字符串型的时间戳,所以简单的写了一个数据类型转换工具类来进行数据类型转换。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class  DataConvertUtil {
	
	//一天的毫秒数
	public static final long ONE_DAY_MILLIS = 24 * 3600000;
	
	/**
	 * 时间戳转字符串显示
	 * 
	 * @param timestamp 要转换的时间戳
	 * @param format 转换格式
	 * @return 转换后字符串
	 */
	public static String Timestamp2String(String timestamp,String format){
		String result = "";
		if(timestamp != null && !timestamp.equals("")){
			String temp = "yyyy-MM-dd HH:mm:ss";
			if(format != null && !format.equals("")){
				temp = format;
			}
			Date date = new Date(Long.valueOf(timestamp));
			SimpleDateFormat sdf = new SimpleDateFormat(temp);
			result = sdf.format(date);
		}
		return result;
	}
	
	/**
	 * 字符串转时间戳
	 * 
	 * @param string 要转换的字符串
	 * @param format 转换格式
	 * @return 转换后的时间戳
	 */
	public static String String2Timestamp(String string,String format){
		String result= "";
		if(string != null && !string.equals("")){
			String temp = "yyyy-MM-dd HH:mm:ss";
			if(format != null && !format.equals("")){
				temp = format;
			}
			SimpleDateFormat sdf = new SimpleDateFormat(temp);
			try {
				Date date = sdf.parse(string);
				result = String.valueOf(date.getTime());
			} catch (ParseException e) {
				ARE.getLog().error("DataConvertUtil类生成日期类型失败");
				return result;
			}
		}
		return result;
	}
	
	/**
	 * 根据时间戳转化成对应周的某一天的时间戳
	 * 
	 * @param timestamp 基准时间戳
	 * @param dayNumber 1-7对应周一到周日
	 * @param format 转化过程中解析格式,默认"yyyy-MM-dd HH:mm:ss"
	 * @param option 特殊配置参数,0表示时分秒均为零,1表示24时0分0秒
	 * @return 对应日期的时间戳
	 */
	public static String Timestamp2WeekDate(String timestamp,int dayNumber,String format,String option) {
		String result = "";
		if(timestamp != null && !timestamp.equals("")){
			String temp = "yyyy-MM-dd HH:mm:ss";
			if(format != null && !format.equals("")){
				temp = format;
			}
			Date date = new Date(Long.valueOf(timestamp));
			int currentNumber = date.getDay();
			Date targetDate = new Date();
			targetDate.setTime(date.getTime() + (dayNumber - currentNumber) * ONE_DAY_MILLIS);
			SimpleDateFormat sdf = new SimpleDateFormat(temp);
			String dateString = sdf.format(targetDate);
			if("0".equals(option)){
				dateString = dateString.split(" ")[0] + " 00:00:00";
			}
			if("1".equals(option)){
				dateString = dateString.split(" ")[0] + " 24:00:00";
			}
			try {
				System.out.println("DataConvertUtil:"+dateString);
				result = String.valueOf(sdf.parse(dateString).getTime());
			} catch (ParseException e) {
				ARE.getLog().error("DataConvertUtil类生成日期类型失败");
				return result;
			}
		}
		return result;
	}
	
	/**
	 * 根据时间戳转化成某月的第一天
	 * 
	 * @param timestamp 基准时间戳
	 * @param format 转化过程中解析格式,默认"yyyy-MM-dd HH:mm:ss"
	 * @param offset 对应当前月的相对偏移量
	 * @return 对应日期的时间戳
	 */
	public static String Timestamp2MonthFirstDay(String timestamp,String format,int offset){
		String result = "";
		if(timestamp != null && !timestamp.equals("")){
			String temp = "yyyy-MM-dd HH:mm:ss";
			if(format != null && !format.equals("")){
				temp = format;
			}
			Date date = new Date(Long.valueOf(timestamp));
			SimpleDateFormat sdf = new SimpleDateFormat(temp);
			String yearMonth = sdf.format(date).replaceAll("-\\d{2} .*", "");
			String year = yearMonth.split("-")[0];
			String month = yearMonth.split("-")[1];
			if(month.contains("0")){
				month = month.substring(1, 2);
			}
			int tempMonth = Integer.valueOf(month) + offset%12;
			year = String.valueOf(Integer.valueOf(year) + offset/12);
			month = String.valueOf(tempMonth);
			if(tempMonth > 12){
				year = String.valueOf(Integer.valueOf(year) + 1);
				month = String.valueOf(tempMonth%12);
			}
			if(tempMonth <= 0){
				year = String.valueOf(Integer.valueOf(year) - 1);
				month = String.valueOf(tempMonth+12);
			}
			result = year + "-" + (month.length()>1?month:("0"+month)) + "-00 00:00:00";
		}
		return result;
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值