Can someone tell me how to convert this string 2012-01-01T08:44:36+01:00
to 2012-01-01 08:44:36
in Java?
Thanks
Can someone tell me how to convert this string 2012-01-01T08:44:36+01:00
to 2012-01-01 08:44:36
in Java?
Thanks
Use SimpleDateFormat
class to format your date.
String value = "2012-01-01T08:44:36+01:00";
String date = null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try
{
Date d = format.parse(value);
date = format.format(d);
}
catch (ParseException ex)
{
System.out.println(ex);
}
return date
More info on http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
But remember that, SimpleDateFormat is not thread-safe. You can use FastDateFormat from apache: FastDateFormat
Usage:
// create a formatter that simply prints the year and month
FastDateFormat formatter =
new FastDateFormat( "yyyy-MM",
TimeZone.getDefault( ),
Locale.getDefault( ) );
// output equals "2012-02"
String output = formatter.format( new Date( ) );
Parsing date:
FastDateFormat formatter = FastDateFormat.getInstance("yyyy-MM");
String s = "2012-01-01T08:44:36+01:00";
try {
Object o = DateUtils.parseDate(s, new String[]{"yyyy-MM-dd'T'HH:mm:ss+01:00"});
// output equals "2012-01"
String output = formatter.format(o);
} catch (ParseException e) {
System.err.println("Bad date: " + s);
}
Sometimes the simplest is best
String s = "2012-01-01T08:44:36+01:00";
// s2 = "2012-01-01 08:44:36"
String s2 = s.substring(0,10) + ' ' + s.substring(11, 19);
Sometimes the simplest is best
String s = "2012-01-01T08:44:36+01:00"; // s2 = "2012-01-01 08:44:36" String s2 = s.substring(0,10) + ' ' + s.substring(12, 8);
This way may produce bugs with indexes, that may appeare, as in your example :-) Also in case of some bad string you will spend a lot of time while finding the bug, because this simple way don't have any checks for input string.
Right indexes:
String s2 = s.substring(0,10) + ' ' + s.substring(11, 19);
If you want to work with date as with string, I prefer using RegXP:
// You can parse your xml output using regXp:
String s = "<root><date>2012-01-01T08:44:36+01:00</date></root>";
Pattern pattern = Pattern.compile("(.+)(T)(.+)([+-])");
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
System.out.println(matcher.group(1) + ' ' + matcher.group(3));
} else {
System.err.println("Bad date: " + s);
}
Speed testing:
public class tempClass {
static final String s = "2012-01-01T08:44:36+01:00".intern();
static String formattedDate = null;
static final int loop = 5 * 1000;
static final String[] patterns = new String[]{"yyyy-MM-dd'T'HH:mm:ss+01:00".intern()};
public static void main(String[] args) throws ParseException, InterruptedException {
testSimpleWay();
testRegXp();
testSimpleDateFormat();
testFastDateFormat();
}
private static void testRegXp() {
Pattern pattern = Pattern.compile("(.+)(T)(.+)([+-])");
Matcher matcher;
final long timeStamp = System.currentTimeMillis();
for (int i = 0; i < loop; i++) {
matcher = pattern.matcher(s);
if (matcher.find()) {
formattedDate = matcher.group(1) + ' ' + matcher.group(3);
} else {
System.err.println("Bad date: " + s);
}
}
System.out.println("Duration of RegXP: " + (System.currentTimeMillis() - timeStamp) + " ms.");
}
private static void testSimpleWay() {
long timeStamp = System.currentTimeMillis();
for (int i = 0; i < loop; i++) {
formattedDate = s.substring(0, 10) + ' ' + s.substring(11, 19);
}
System.out.println("Duration of Simple way: " + (System.currentTimeMillis() - timeStamp) + " ms.");
}
private static void testSimpleDateFormat() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
final long timeStamp = System.currentTimeMillis();
for (int i = 0; i < loop; i++) {
try {
formattedDate = simpleDateFormat.format((Date) DateUtils.parseDate(s, patterns));
} catch (ParseException ex) {
System.err.println("Bad date: " + s);
}
}
System.out.println("Duration of SimpleDateFormat: " + (System.currentTimeMillis() - timeStamp) + " ms.");
}
private static void testFastDateFormat() {
FastDateFormat fastDateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
final long timeStamp = System.currentTimeMillis();
for (int i = 0; i < loop; i++) {
try {
formattedDate = fastDateFormat.format((Date) DateUtils.parseDate(s, patterns));
} catch (ParseException e) {
System.err.println("Bad date: " + s);
}
}
System.out.println("Duration of FastDateFormat: " + (System.currentTimeMillis() - timeStamp) + " ms.");
}
Output:
Duration of Simple way: 5 ms.
Duration of RegXP: 20 ms.
Duration of SimpleDateFormat: 114 ms.
Duration of FastDateFormat: 65 ms.