source: josm/trunk/src/org/openstreetmap/josm/data/osm/DateFormatter.java@ 627

Last change on this file since 627 was 627, checked in by framm, 16 years ago
  • Property svn:eol-style set to native
File size: 1.6 KB
Line 
1package org.openstreetmap.josm.data.osm;
2
3import java.util.Calendar;
4import java.util.Date;
5import java.util.GregorianCalendar;
6import java.util.TimeZone;
7
8
9/**
10 * Outputs a date in a format suitable for an OSM XML file.
11 *
12 * @author Brett Henderson
13 */
14public class DateFormatter {
15
16 private GregorianCalendar calendar;
17
18
19 /**
20 * Creates a new instance.
21 */
22 public DateFormatter() {
23 calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
24 }
25
26
27 /**
28 * Formats a date in XML format.
29 *
30 * @param date
31 * The date to be formatted.
32 * @return The string representing the date.
33 */
34 public String format(Date date) {
35 StringBuilder result;
36 int year;
37 int month;
38 int day;
39 int hour;
40 int minute;
41 int second;
42
43 calendar.setTime(date);
44
45 result = new StringBuilder(20);
46
47 year = calendar.get(Calendar.YEAR);
48 month = calendar.get(Calendar.MONTH) + 1;
49 day = calendar.get(Calendar.DATE);
50 hour = calendar.get(Calendar.HOUR_OF_DAY);
51 minute = calendar.get(Calendar.MINUTE);
52 second = calendar.get(Calendar.SECOND);
53
54 result.append(year);
55 result.append('-');
56 if (month < 10) {
57 result.append('0');
58 }
59 result.append(month);
60 result.append('-');
61 if (day < 10) {
62 result.append('0');
63 }
64 result.append(day);
65 result.append('T');
66 if (hour < 10) {
67 result.append('0');
68 }
69 result.append(hour);
70 result.append(':');
71 if (minute < 10) {
72 result.append('0');
73 }
74 result.append(minute);
75 result.append(':');
76 if (second < 10) {
77 result.append('0');
78 }
79 result.append(second);
80 result.append('Z');
81
82 return result.toString();
83 }
84}
Note: See TracBrowser for help on using the repository browser.