/*
* DeliciousDroid - http://code.google.com/p/DeliciousDroid/
*
* Copyright (C) 2010 Matt Schmidt
*
* DeliciousDroid is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 3 of the License,
* or (at your option) any later version.
*
* DeliciousDroid is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DeliciousDroid; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
package com.deliciousdroid.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class DateParser {
public static final TimeZone tz = TimeZone.getTimeZone("GMT");
public static final Calendar c = Calendar.getInstance(tz);
public static Date parse( String input ) throws java.text.ParseException {
//NOTE: SimpleDateFormat uses GMT[-+]hh:mm for the TZ which breaks
//things a bit. Before we go on we have to repair this.
SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssz" );
//this is zero time so we need to add that TZ indicator for
if ( input.endsWith( "Z" ) ) {
input = input.substring( 0, input.length() - 1) + "GMT-00:00";
} else {
int inset = 6;
String s0 = input.substring( 0, input.length() - inset );
String s1 = input.substring( input.length() - inset, input.length() );
input = s0 + "GMT" + s1;
}
return df.parse( input );
}
public static long parseTime( String input ) {
c.set(IntUtils.parseUInt(input.substring(0, 4)),
IntUtils.parseUInt(input.substring(5, 7)) - 1,
IntUtils.parseUInt(input.substring(8, 10)),
IntUtils.parseUInt(input.substring(11, 13)),
IntUtils.parseUInt(input.substring(14, 16)),
IntUtils.parseUInt(input.substring(17, 19)));
return c.getTimeInMillis();
}
public static String toString( Date date ) {
SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssz" );
TimeZone tz = TimeZone.getTimeZone( "UTC" );
df.setTimeZone( tz );
String output = df.format( date );
int inset0 = 9;
int inset1 = 6;
String s0 = output.substring( 0, output.length() - inset0 );
String s1 = output.substring( output.length() - inset1, output.length() );
String result = s0 + s1;
result = result.replaceAll( "UTC", "+00:00" );
return result;
}
}