java中字符串與日期的轉(zhuǎn)換實(shí)例
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateIO {
public static void main(String[] args) {
Date date= new DateIO().strToDate("2013-04-01");
String strdate=new DateIO().dateToStr(date);
String srrdate=new DateIO().timestampToStr(System.currentTimeMillis());
Timestamp ts=new DateIO().strToTimestamp(new Date());
}
//String 轉(zhuǎn)換為 Date
public Date strToDate(String strdate){
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = format.parse(strdate);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("date:"+date);
return date;
}
//Date 轉(zhuǎn)換為 String
public String dateToStr(Date date){
//年月日****-**-**
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String str = format.format(date);
System.out.println("str:"+str);
//年月日**-*-*
format = DateFormat.getDateInstance(DateFormat.SHORT);
str = format.format(date);
System.out.println(str);
//年月日****-*-*
format = DateFormat.getDateInstance(DateFormat.MEDIUM);
str = format.format(date);
System.out.println(str);
//****年*月*日星期*
format = DateFormat.getDateInstance(DateFormat.FULL);
str = format.format(date);
System.out.println(str);
return str;
}
//Timestamp轉(zhuǎn)換為String
public String timestampToStr(Long timestamp){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定義格式,不顯示毫秒
String str = df.format(timestamp);
System.out.println(str);
return str;
}
//Date轉(zhuǎn)換為Timestamp
public Timestamp strToTimestamp(Date date){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = df.format(date);
Timestamp ts = Timestamp.valueOf(time);
System.out.println(ts);
return ts;
}
}
相關(guān)文章
Spring boot 使用JdbcTemplate訪問數(shù)據(jù)庫
SpringBoot 是為了簡(jiǎn)化 Spring 應(yīng)用的創(chuàng)建、運(yùn)行、調(diào)試、部署等一系列問題而誕生的產(chǎn)物。本文重點(diǎn)給大家介紹spring boot 使用JdbcTemplate訪問數(shù)據(jù)庫,需要的朋友可以參考下2018-05-05使用Jenkins自動(dòng)化構(gòu)建工具進(jìn)行敏捷開發(fā)
這篇文章主要為大家介紹了使用Jenkins自動(dòng)化構(gòu)建工具進(jìn)行敏捷開發(fā),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04SpringBoot Security實(shí)現(xiàn)單點(diǎn)登出并清除所有token
Spring Security是一個(gè)功能強(qiáng)大且高度可定制的身份驗(yàn)證和訪問控制框架。提供了完善的認(rèn)證機(jī)制和方法級(jí)的授權(quán)功能。是一款非常優(yōu)秀的權(quán)限管理框架。它的核心是一組過濾器鏈,不同的功能經(jīng)由不同的過濾器2023-01-01javacv視頻抽幀的實(shí)現(xiàn)過程詳解(附代碼)
這篇文章主要介紹了javacv視頻抽幀的實(shí)現(xiàn)過程詳解(附代碼),視頻抽幀可以做一些處理,比如水印,去水印等操作,然后再合成視頻,需要的朋友可以參考下2019-07-07idea自動(dòng)加載html、js而無需重啟進(jìn)程的操作
這篇文章主要介紹了idea自動(dòng)加載html、js而無需重啟進(jìn)程的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08