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

来源:互联网 发布:2016年淘宝交易额 编辑:程序博客网 时间:2024/06/06 08:41

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

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;}}

原创粉丝点击