时间戳&date&String转化工具
来源:互联网 发布:php在数组中查找 编辑:程序博客网 时间:2024/06/03 20:52
package com.xc.utils;import java.sql.Timestamp;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;import org.apache.commons.lang.StringUtils; /** * 时间转化工具 date转为时间戳 时间戳转date 互相与String的转换 * 所有出现的String time 格式都必须为(yyyy-MM-dd HH:mm:ss),否则出错 * @author GuoMing * */public class TimeExchange { /** * String(yyyy-MM-dd HH:mm:ss) 转 Date */ public static Date StringToDate(String time) throws ParseException { if(StringUtils.equals(time, "") || time == null) return null; Date date = new Date(); // 注意format的格式要与日期String的格式相匹配 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { date = dateFormat.parse(time); } catch (Exception e) { e.printStackTrace(); } return date; } /** * Date转为String(yyyy-MM-dd HH:mm:ss) * * @param time * @return */ public static String DateToString(Date time) { String dateStr = ""; Date date = new Date(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH/mm/ss"); try { dateStr = dateFormat.format(time); System.out.println(dateStr); } catch (Exception e) { e.printStackTrace(); } return dateStr; } public static String DateToStringSimple(Date time) { String dateStr = ""; Date date = new Date(); DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); try { dateStr = dateFormat.format(time); System.out.println(dateStr); } catch (Exception e) { e.printStackTrace(); } return dateStr; } /** * String(yyyy-MM-dd HH:mm:ss)转10位时间戳 * @param time * @return */ public static Integer StringToTimestamp(String time){ int times = 0; try { times = (int) ((Timestamp.valueOf(time).getTime())/1000); } catch (Exception e) { e.printStackTrace(); } if(times==0){ System.out.println("String转10位时间戳失败"); } return times; } /** * 10位int型的时间戳转换为String(yyyy-MM-dd HH:mm:ss) * @param time * @return */ public static String timestampToString(Integer time){ //int转long时,先进行转型再进行计算,否则会是计算结束后在转型 long temp = (long)time*1000; Timestamp ts = new Timestamp(temp); String tsStr = ""; DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { //方法一 tsStr = dateFormat.format(ts); System.out.println(tsStr); } catch (Exception e) { e.printStackTrace(); } return tsStr; } /** * 10位时间戳转Date * @param time * @return */ public static Date TimestampToDate(Integer time){ long temp = (long)time*1000; Timestamp ts = new Timestamp(temp); Date date = new Date(); try { date = ts; //System.out.println(date); } catch (Exception e) { e.printStackTrace(); } return date; } /** * Date类型转换为10位时间戳 * @param time * @return */ public static Integer DateToTimestamp(Date time){ Timestamp ts = new Timestamp(time.getTime()); return (int) ((ts.getTime())/1000); } /** * */ public static int getCurrentTimestamp(){ return (int) (System.currentTimeMillis() / 1000); } public static String getCurrentString(){DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");return dateFormat.format(new Date());} public static Date getDate(String str) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = null;try {date = sdf.parse(str);} catch (Exception e) {try {date = sdf2.parse(str);} catch (ParseException e1) {try {date = sdf3.parse(str);} catch (ParseException e2) {}}}return date;}public static String getDate(long l) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());String str = sdf.format(new Date(l));return str;}/** * 用于月份充值时满一年,年份加1 判断 * 传入2017年9月,4 返回 2018年1月 * @Description * @author GuoMing * @date 2017年6月15日 下午1:50:39 * @param now * @param apiTime */public static Date getAddMonthDate(Date now, int monthNum) {Date newDate=now;int year=now.getYear();int month=now.getMonth();if(monthNum+month>=12){year+=1;month=monthNum+month-12;}else{month=monthNum+month;}newDate.setMonth(month);newDate.setYear(year);return newDate;}//返回当前日期的前m个月1号 日期public static String beforeMonth(int m){Date d=new Date();int year=d.getYear();int month=d.getMonth();if((month-m)==0){year=year-1;month=12;}else if((month-m)<0){month=month-m+12;year=year-1;}else{month=month-m;}Date date=new Date(year,month,1);SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());String str = sdf.format(date);return str;}}个人常用网址收藏管理——666导航网
阅读全文
0 0
- 时间戳&date&String转化工具
- java时间戳与Date(String)相互转化
- Date&String时间转换工具类
- 【Date】时间戳转化成Date时间格式处理
- Java获取当前时间及String、datetime、date相互转化
- JAVA String时间转化为数据库Date类型
- Java获取当前时间及String、datetime、date相互转化
- Date,String,Timestamp转化
- java 时间戳与Date相互转化
- Java时间戳与Date相互转化
- 时间戳字符串转化为date格式
- date时间转化
- 时间戳在线转化工具
- JS 时间格式化 对Date的扩展,将 Date 转化为指定格式的String
- Java日期格式转化工具(date格式转为string格式,string转date,string转timestamp类型)
- 时间格式字符串转化为date和时间戳
- String转换成时间戳(String Date 时间戳)
- Java String 转化为 Date
- linq union 抛出distinct异常
- Meanshift和Camshift算法总结
- iOS app被拒------3.2(f)开发者账号被封
- 投票功能的一点思考
- 金融金句(收藏使用)
- 时间戳&date&String转化工具
- ddms 的data文件夹没东西?(怕不是没打开哦) 此为针对Android studio实现,应该也可以用在某过气IDE上
- kotlin学习记录
- Linux全自动安装wdcp面板脚本
- 搭建个人博客
- 大型系统持续集成持续交付解决方案及案例
- modem manager与network manager
- android studio 2.3 打aar包
- PHP 编程中 10 个最常见的错误,你犯过几个?