get Current Date By Offset and calendar field - Android java.util

Android examples for java.util:Calendar

Description

get Current Date By Offset and calendar field

Demo Code

import android.text.TextUtils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main{

    public static String getCurrentDateByOffset(String format,
            int calendarField, int offset) {
        String mDateTime = null;/*from   w ww. ja va 2  s  . c om*/
        try {
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(
                    format);
            Calendar c = new GregorianCalendar();
            c.add(calendarField, offset);
            mDateTime = mSimpleDateFormat.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mDateTime;

    }

}

Related Tutorials