/* * Copyright 2012 Tallinn University Centre for Educational Technology * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package ee.htk.dippler.utils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import android.content.Context; import android.text.format.DateFormat; import android.util.Log; public class ServiceDate { public static final String DATE_FORMAT = "EEE MMM dd kk:mm:ss z yyyy"; public static final String DATE_FORMAT_WP = "EEE, dd MMM yyyy kk:mm:ss z"; static java.text.DateFormat df; static SimpleDateFormat sdf = new SimpleDateFormat(ServiceDate.DATE_FORMAT, Locale.US); public static String format(Context context, String date) { Log.i("dippler-app", "Current date in " + date); if ( df == null ) { df = DateFormat.getLongDateFormat(context); } //try if integer passed try { int seconds = Integer.parseInt(date); return ServiceDate.format(context, seconds); } catch (NumberFormatException e) { //Nothing todo } try { date = df.format(sdf.parse(date)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.i("dippler-app", "Date out " + date); return date; } public static String format(Context context, int timestamp) { if ( df == null ) { df = DateFormat.getLongDateFormat(context); } return df.format(new Date( timestamp * 1000L )); } public static String format(Context context, Date date_in) { if ( df == null ) { df = DateFormat.getLongDateFormat(context); } return df.format(date_in); } public static String format(Context context, String date, String dateFormat) { if ( df == null ) { df = DateFormat.getLongDateFormat(context); } SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.US); try { return df.format(sdf.parse(date)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return date; } }