1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.efaps.util;
22
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.sql.Statement;
26 import java.sql.Timestamp;
27 import java.util.Date;
28
29 import org.efaps.admin.EFapsSystemConfiguration;
30 import org.efaps.admin.KernelSettings;
31 import org.efaps.db.Context;
32 import org.efaps.db.transaction.ConnectionResource;
33 import org.joda.time.DateTime;
34 import org.joda.time.DateTimeZone;
35 import org.joda.time.chrono.ISOChronology;
36 import org.joda.time.format.ISODateTimeFormat;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41
42
43
44
45
46
47 public final class DateTimeUtil
48 {
49
50
51
52 private static final Logger LOG = LoggerFactory.getLogger(DateTimeUtil.class);
53
54
55
56
57
58 private DateTimeUtil()
59 {
60 }
61
62
63
64
65
66
67
68 public static Timestamp getCurrentTimeFromDB() throws EFapsException
69 {
70 Timestamp now = null;
71 final ConnectionResource rsrc = Context.getThreadContext().getConnectionResource();
72 Statement stmt;
73 try {
74 stmt = rsrc.getConnection().createStatement();
75 final ResultSet resultset = stmt.executeQuery("SELECT " + Context.getDbType().getCurrentTimeStamp());
76 resultset.next();
77 now = resultset.getTimestamp(1);
78 resultset.close();
79 stmt.close();
80 rsrc.commit();
81 } catch (final SQLException e) {
82 DateTimeUtil.LOG.error("could not execute SQL-Statement", e);
83 }
84 return now;
85 }
86
87
88
89
90
91
92
93
94
95
96
97 public static DateTime normalize(final DateTime _date)
98 throws EFapsException
99 {
100
101 final String timezoneID = EFapsSystemConfiguration.get().getAttributeValue(KernelSettings.DBTIMEZONE);
102 final ISOChronology chron;
103 if (timezoneID != null) {
104 final DateTimeZone timezone = DateTimeZone.forID(timezoneID);
105 chron = ISOChronology.getInstance(timezone);
106 } else {
107 chron = ISOChronology.getInstanceUTC();
108 }
109 return _date.withChronology(chron);
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123 public static DateTime translateFromUI(final Object _value)
124 throws EFapsException
125 {
126 final DateTime ret;
127
128 final String timezoneID = EFapsSystemConfiguration.get().getAttributeValue(KernelSettings.DBTIMEZONE);
129 final ISOChronology chron;
130 if (timezoneID != null) {
131 final DateTimeZone timezone = DateTimeZone.forID(timezoneID);
132 chron = ISOChronology.getInstance(timezone);
133 } else {
134 chron = ISOChronology.getInstanceUTC();
135 }
136 if (_value instanceof Date) {
137 ret = new DateTime(_value).withChronology(chron);
138 } else if (_value instanceof DateTime) {
139 ret = ((DateTime) _value).withChronology(chron);
140 } else if (_value instanceof String) {
141 ret = ISODateTimeFormat.dateTime().parseDateTime((String) _value).withChronology(chron);
142 } else {
143 ret = null;
144 }
145 return ret;
146 }
147 }