欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAVA Date String 转 timestamp

程序员文章站 2022-03-17 19:02:36
...
1.当前日转String转时间戳
Date dt=new Date();
SimpleDateFormat matter1=new SimpleDateFormat("yyyy-MM-dd");
System.out.println(matter1.format(dt));
		     
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Timestamp ts = new Timestamp(format.parse(matter1.format(dt)).getTime());
System.out.println(ts.toString());



2.当前日期转时间戳
java.util.Date today = new java.util.Date();  
return new java.sql.Timestamp(today.getTime());


3.时间戳转String
    public static String timestamp2Str(Timestamp tp) {
    	if (tp == null) {
    		return "";
    	}
    	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	String str = df.format(tp);
    	return str;
    }


timestamp to Date

JAVA Date String 转 timestamp
            
    
    博客分类: JAVA JAVAString转timestamp 



比较


<code>
String DateStr1 = "2014-10-1 10:20:16";
String DateStr2 = "2014-10-07 15:50:35";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dateTime1 = dateFormat.parse(DateStr1);
Date dateTime2 = dateFormat.parse(DateStr2);
int i = dateTime1.compareTo(dateTime2);
System.out.println(i < 0);

</code>
  • JAVA Date String 转 timestamp
            
    
    博客分类: JAVA JAVAString转timestamp 
  • 大小: 6.3 KB