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

Last change on this file since 2667 was 2512, checked in by stoecker, 14 years ago

i18n updated, fixed files to reduce problems when applying patches, fix #4017

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