/
DateUtils.java
119 lines (111 loc) · 3.48 KB
/
DateUtils.java
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
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.xoai.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
/**
*
* @author Lyncode Development Team <dspace@lyncode.com>
*/
public class DateUtils
{
private static Logger log = LogManager.getLogger(DateUtils.class);
public static String formatToSolr(Date date)
{
return formatToSolr(date, true);
}
public static String formatToSolr(Date date, boolean init)
{
// 2008-01-01T00:00:00Z
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.'000Z'");
if (!init) sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.'999Z'");
String ret = sdf.format(date);
System.out.println(ret);
return ret;
}
public static Date parseDate(String date)
{
// 2008-01-01T00:00:00Z
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.getDefault());
// format.setTimeZone(TimeZone.getTimeZone("ZULU"));
Date ret;
try
{
ret = format.parse(date);
return ret;
}
catch (ParseException e)
{
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
Locale.getDefault());
try
{
return format.parse(date);
}
catch (ParseException e1)
{
format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
try
{
return format.parse(date);
}
catch (ParseException e2)
{
format = new SimpleDateFormat("yyyy-MM",
Locale.getDefault());
try
{
return format.parse(date);
}
catch (ParseException e3)
{
format = new SimpleDateFormat("yyyy",
Locale.getDefault());
try
{
return format.parse(date);
}
catch (ParseException e4)
{
log.error(e4.getMessage(), e);
}
}
}
}
}
return new Date();
}
public static Date parseFromSolrDate(String date)
{
// 2008-01-01T00:00:00Z
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.getDefault());
// format.setTimeZone(TimeZone.getTimeZone("UTC"));
Date ret;
try
{
ret = format.parse(date);
return ret;
}
catch (ParseException e)
{
log.error(e.getMessage(), e);
}
return new Date();
}
public static Date toSolrDate (Date date) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Date indexFormatExpiryDate = sdf.parse(sdf.format(date));
return indexFormatExpiryDate;
}
}