java字符串与日期类型转换的工具类

时间:2022-09-24 17:17:11

常用的字符串转date,和日期转字符串的方法,具体内容如下

java" id="highlighter_226307">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.cq2022.zago.base.util;
 
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
 
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
/***
 * 日期工具类
 *
 * @author shijing
 *
 */
public class DateUtils {
 
 private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);
  
 /***
  * Date类型转换成XMLGregorianCalendar类型
  *
  * @param date
  * @return
  */
 public static XMLGregorianCalendar convertToXMLGregorianCalendar(Date date) {
  GregorianCalendar cal = new GregorianCalendar();
  cal.setTime(date);
  XMLGregorianCalendar gc = null;
  try {
   gc = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
  }
  catch (Exception e) {
   logger.error("Date类型转换成XMLGregorianCalendar类型出错:"+e);
  }
  return gc;
 }
 
 /***
  * XMLGregorianCalendar类型转换成Date类型
  *
  * @param cal
  * @return
  * @throws Exception
  */
 public static Date convertToDate(XMLGregorianCalendar cal) throws Exception {
  GregorianCalendar ca = cal.toGregorianCalendar();
  return ca.getTime();
 }
 
 /**
  * String 转 Date
  * 2015年3月25日上午11:27:14
  * auther:shijing
  * @param str 日期字符串
  * @param format 转换格式
  * @return
  * Date
  */
 public static Date stringToDate(String str, String format) {
  DateFormat dateFormat = new SimpleDateFormat(format);
  Date date = null;
  try {
   date = dateFormat.parse(str);
  }
  catch (ParseException e) {
   logger.error("String类型 转 Date类型出错:"+e);
  }
  return date;
 }
  
 /**
  * Date 转 String
  * auther: shijing
  * 2015年3月25日上午11:28:14
  * @param date 日期
  * @param format 转换格式
  * @return
  */
 public static String dateToString(Date date,String format){
  DateFormat dateFormat = new SimpleDateFormat(format);
  String strDate=null;
  try {
   if(date!=null){
    strDate=dateFormat.format(date);
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   logger.error("Date类型 转 String类型出错:"+e);
  }
  return strDate;
 }
  
 
 /***
  * 测试方法
  *
  * @param args
  */
 public static void main(String[] args) {
  XMLGregorianCalendar d = DateUtils
    .convertToXMLGregorianCalendar(new Date());
//  System.out.println(d.getDay());
  XMLGregorianCalendar cal = null;
  try {
   cal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
   cal.setMonth(06);
   cal.setYear(2010);
   Date date = DateUtils.convertToDate(cal);
   String format = "yyyy-MM-dd HH:mm:ss";
   SimpleDateFormat formatter = new SimpleDateFormat(format);
//   System.out.println(formatter.format(date));
    
   Date d1 = DateUtils.stringToDate("2014/7/24 9:51:00", "yyyy/MM/dd hh:mm:ss");
   XMLGregorianCalendar d2 = DateUtils.convertToXMLGregorianCalendar(d1);
//   System.out.println(d2.toString());
    
   String dateStr=DateUtils.dateToString(cal.toGregorianCalendar().getTime(), "yyyy-MM-dd HH:mm:ss");
//   System.out.println("dateStr="+ dateStr);
  }
  catch (Exception e) {
   e.printStackTrace();
  }
 }
}

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/moneyshi/article/details/44852157