Returns a Date object for the date and time (simple format) that is localized to the users current timezone. - Android java.util

Android examples for java.util:Timezone

Description

Returns a Date object for the date and time (simple format) that is localized to the users current timezone.

Demo Code

import android.content.Context;
import android.text.format.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main{

    /**//from  ww  w . jav  a 2 s.  com
     * <p>Returns a Date object for the date and time (simple format) that is localized to the 
     * users current timezone.  It also compensates for the offset from UTC that the
     * timestamps are stored in.</p>
     * 
     * <p>It should be used to format any time needing manipulation that will be output 
     * to the view layer.</p>
     * 
     * @param date
     * @param context
     * @return Date
     */
    public static Date getLocalizedDateTime(Date date, Context context) {
        java.text.DateFormat df = DateFormat.getDateFormat(context);
        java.text.DateFormat tf = DateFormat.getTimeFormat(context);
        TimeZone tz = TimeZone.getDefault();
        Date modTime = new Date(date.getTime()
                + tz.getOffset(date.getTime()));
        return modTime;
    }

}

Related Tutorials