标签:系统 dateutil string转换 date .text 转换 之间 code 方法
经常在开发的过程中遇到这样的问题,从数据库中读出来的数据需要转换为对像或者java bean,此时经常使用到setDate(Date date);这样的方法。感觉这是个很简单而又难受的事情,在这里浪费时间真的是不应该。我这里就记录下我使用过的,方便以后查阅使用。
例如下面的setter方法:
private String userId; private boolean isLogin; private Date loginTime; public void setLoginTime(Date loginTime) { Date oldLoginTime = this.loginTime; this.loginTime = loginTime; } public void setIsLogin(boolean isLogin) { boolean oldIsLogin = this.isLogin; this.isLogin = isLogin; } public void setUserId(String userId) { String oldUserId = this.userId; this.userId = userId; }
使用到的String转换为date方法
import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateUtil { /** * set方法中是timestamp类型 * new Timestamp(s) * @return s */ public Timestamp newStampTime(){ long s=System.currentTimeMillis(); return new Timestamp(s); } /** * timeStamp转换为String * @return */ public String timeStampToString(){ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式,不显示毫秒 Timestamp now = new Timestamp(System.currentTimeMillis());//获取系统当前时间 String str = df.format(now); return str; } /** * String 转换为timestamp * @return */ public Timestamp StringTotimeStamp(){ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = df.format(new Date()); Timestamp ts = Timestamp.valueOf(time); return ts; } public Date myStringTotimeStamp(String dateString){ int length=dateString.length(); if(length>10){ DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date today=new Date(); try { today = df.parse("2009-11-11"); //String ts=df.format(today); } catch (ParseException e) { e.printStackTrace(); } return today; }else { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date today=new Date(); try { today = df.parse("2009-11-11"); //String ts=df.format(today); } catch (ParseException e) { e.printStackTrace(); } return today; } } /** * String 转换为timestamp * String "2016-5-25" 转换为 Timestamp * @return */ public Timestamp StringTotimeStamp(String dateString){ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); StringBuffer ds=new StringBuffer(dateString); if(dateString.length()>10){ Timestamp ts = Timestamp.valueOf(dateString); return ts; }else{ ds.append(" 00:00:00"); Timestamp ts = Timestamp.valueOf(ds.toString()); return ts; } } /** * 获取当前时间字符串 * eg:2016-10-11 16:57:52 **/ public String formateDateString(){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s"); String dateString=format.format(new Date()); return dateString; } /** * 指定时间和当前时间比较大小 */ public boolean compareTime(String t1){ if(t1.length()<=10){ t1=t1+" 23:59:59"; } Date currentTime = new Date();// 当前时间 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String nowtime = formatter.format(currentTime); Calendar now = Calendar.getInstance(); Calendar c1 = Calendar.getInstance(); //String t1 = "2016-02-29 00:30:00"; try { now.setTime(formatter.parse(nowtime)); c1.setTime(formatter.parse(t1)); } catch (ParseException e) { e.printStackTrace(); } int result1 = now.compareTo(c1);// 比开始时间小,未开始 System.out.println("结果:"+result1); if(result1>0){ return false; }else{ return true; } } }
java中setDate(Date date)方法和String与Date之间的转换
标签:系统 dateutil string转换 date .text 转换 之间 code 方法
原文地址:http://www.cnblogs.com/zhang-cb/p/6111742.html