经常在开发的过程中遇到这样的问题,从数据库中读出来的数据需要转换为对像或者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之间的转换的更多相关文章

  1. 深入理解Java中的同步静态方法和synchronized(class)代码块的类锁

    一.回顾学习内容 在前面几篇博客中我我们已经理解了synchronized对象锁.对象锁的重入.synchronized方法块.synchronized非本对象的代码块, 链接:https://www ...

  2. Java中Integer类的方法和request的setAttribute方法的使用与理解

    一.Integer类的使用方法 Interger:整数类型 1.属性. static int MAX_VALUE:返回最大的整型数: static int MIN_VALUE:返回最小的整型数: st ...

  3. Java 中 byte、byte 数组和 int、long 之间的转换

    Java 中 byte 和 int 之间的转换源码: //byte 与 int 的相互转换 public static byte intToByte(int x) { return (byte) x; ...

  4. Java中Collections的sort方法和Comparable与Comparator的比较

    一.Comparable 新建Student1类,类实现Comparable接口,并重写compareTo方法 public class Student1 implements Comparable& ...

  5. Java中带参数的方法和JavaScript中带参数的函数有什么不同?

    javascript是动态语言,是弱类型语言,其参数的使用很灵活:java则是强类型语言,参数的类型必须明确的

  6. java中字节数组byte[]和字符(字符串)之间的转换

    转自:http://blog.csdn.net/linlzk/article/details/6566124 Java与其他语言编写的程序进行tcp/ip socket通讯时,通讯内容一般都转换成by ...

  7. java数组、java.lang.String、java.util.Arrays、java.lang.Object的toString()方法和equals()方法详解

    public class Test { public static void main(String[] args) { int[] a = {1, 2, 4, 6}; int[] b = a; in ...

  8. JAVA反射中的getFields()方法和getDeclaredFields ()方法的区别

    JAVA反射中的getFields()方法和getDeclaredFields ()方法的区别   关于获取类的字段有两种方式:getFields()和getDeclaredFields().我们先来 ...

  9. java 中的Number类 Character类 String类 StringBuffer类 StringBuilder类

    1. Number类 Java语言为每一个内置数据类型提供了对应的包装类.所有的包装类(Integer.Long.Byte.Double.Float.Short)都是抽象类Number的子类.这种由编 ...

随机推荐

  1. 导出excel的三种方式

    第一种是Response输出,这种方式输出的文件不符合标准的excel格式,在打开的时候会有提示,而且不好控制内容.第一种是Response输出,这种方式输出的文件不符合标准的excel格式,在打开的 ...

  2. WPF自定义控件与样式(15)-终结篇

    原文:WPF自定义控件与样式(15)-终结篇 系列文章目录  WPF自定义控件与样式(1)-矢量字体图标(iconfont) WPF自定义控件与样式(2)-自定义按钮FButton WPF自定义控件与 ...

  3. 【转】android小结(一)之menu

    原文网址:http://zhouyunan2010.iteye.com/blog/1151215 android提供有三种menu类型 一.Options Menu(选项菜单) 这是一组item选项的 ...

  4. HTTP response codes

    面试被问起了413和503,我觉得也是够BT的,能问出这种无聊的问题.很多返回码几乎很难遇到,不过还是把MDN上很好的描述转过来作为一个reference. HTTP协议状态码表示的意思主要分为五类 ...

  5. CCF 送货 + 欧拉路模板

    #include <bits/stdc++.h> using namespace std; stack<int> st; vector<]; ][]; ],cp[]; i ...

  6. 算法导论(第三版)Problems2(归并插入排序、数列逆序计算)

    讨论内容不说明,仅提供相应的程序. 2.1:归并插入排序θ(nlgn) void mergeInsertionSort(int a[], int l, int r, int k) { int m; & ...

  7. Java组合与继承生成的类中构造函数的执行顺序

    [程序实例] import java.util.*; class Meal{ Meal() { System.out.println("Meal Constructor"); } ...

  8. C#_Test

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Plus ...

  9. MsgBox-官方文档

    http://s3.envato.com/files/293712/index.html

  10. 正则表达式:网页爬虫:从TXT中获取邮箱地址(获取的练习,缺点:一行只能匹配一个)

    import java.util.regex.*; import java.io.*; class L { public static void main(String[] args) throws  ...