java字符串類型和時間類型的轉換


 
         
類型轉換
 
         

 

 //reqeust.getParameter獲取字符串直接賦值
1
public static Date date(String date_str) { 2 try { 3 Calendar zcal = Calendar.getInstance();//日期類 4 Timestamp timestampnow = new Timestamp(zcal.getTimeInMillis());//轉換成正常的日期格式 5 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");//改為需要的東西 6 ParsePosition pos = new ParsePosition(0); 7 java.util.Date current = formatter.parse(date_str, pos); 8 timestampnow = new Timestamp(current.getTime()); 9 return timestampnow; 10 } 11 catch (NullPointerException e) { 12 return null; 13 } 14 }

 

 1 //Date類型轉換成String類型
 2  public static String toJson(Object obj){
 3        String reuqest=null;
 4         //對象映射
 5          ObjectMapper mapper=new ObjectMapper();
 6           //設置時間格式
 7          SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy年MM月dd日");
 8           mapper.setDateFormat(dateFormat);
 9              try {
10                  reuqest=mapper.writeValueAsString(obj);
11              } catch (JsonProcessingException e) {
12                  // TODO Auto-generated catch block
13                 e.printStackTrace();
14             }
15         return reuqest;
16      }

 

 
//String類型轉換成Date類型

1 public static Date date(String date_str) {
 2         try {  3             Calendar zcal = Calendar.getInstance();//日期類
 4             Timestamp timestampnow = new Timestamp(zcal.getTimeInMillis());//轉換成正常的日期格式
 5             SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");//改為需要的東西
 6             ParsePosition pos = new ParsePosition(0);  7             java.util.Date current = formatter.parse(date_str, pos);  8             timestampnow = new Timestamp(current.getTime());  9             return timestampnow; 10  } 11         catch (NullPointerException e) { 12             return null; 13  } 14     }

 

 



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM