1 17 package org.alfresco.util; 18 19 import java.util.Calendar ; 20 import java.util.Date ; 21 import java.util.GregorianCalendar ; 22 23 28 public final class Conversion 29 { 30 33 private Conversion() 34 { 35 } 36 37 44 public static String dateToXmlDate(Date date) 45 { 46 Calendar calendar = new GregorianCalendar (); 47 calendar.setTime(date); 48 49 StringBuilder buf = new StringBuilder (20); 50 51 String month = Integer.toString(calendar.get(Calendar.MONTH) + 1); 53 String day = Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)); 54 String hour = Integer.toString(calendar.get(Calendar.HOUR_OF_DAY)); 55 String minute = Integer.toString(calendar.get(Calendar.MINUTE)); 56 String second = Integer.toString(calendar.get(Calendar.SECOND)); 57 58 buf.append(calendar.get(Calendar.YEAR)) 59 .append('-'); 60 61 if (month.length() == 1) 62 { 63 buf.append('0'); 64 } 65 buf.append(month) 66 .append('-'); 67 68 if (day.length() == 1) 69 { 70 buf.append('0'); 71 } 72 buf.append(day) 73 .append('T'); 74 75 if (hour.length() == 1) 76 { 77 buf.append('0'); 78 } 79 buf.append(hour) 80 .append(":"); 81 82 if (minute.length() == 1) 83 { 84 buf.append('0'); 85 } 86 buf.append(minute) 87 .append(":"); 88 89 if (second.length() == 1) 90 { 91 buf.append('0'); 92 } 93 buf.append(second); 94 95 return buf.toString(); 96 } 97 98 105 public static Date dateFromXmlDate(String xml) 106 { 107 if (xml == null || xml.length() != 19) 108 { 109 return null; 110 } 111 112 int year = Integer.parseInt(xml.substring(0, 4)); 113 int month = Integer.parseInt(xml.substring(5, 7)); 114 int day = Integer.parseInt(xml.substring(8, 10)); 115 int hours = Integer.parseInt(xml.substring(11, 13)); 116 int mins = Integer.parseInt(xml.substring(14, 16)); 117 int secs = Integer.parseInt(xml.substring(17, 19)); 118 119 Calendar calendar = new GregorianCalendar (year, month - 1, day, hours, mins, secs); 120 121 return calendar.getTime(); 122 } 123 } 124 | Popular Tags |