get Today by different format - Android java.util

Android examples for java.util:Today

Description

get Today by different format

Demo Code


//package com.java2s;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static String fm = "yyyy/MM/dd/";
    public static String fm2 = "MM/dd/";
    public static String fm3 = "yyyy/MM/";

    public static String getToday() {
        Calendar cal = Calendar.getInstance();
        return String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
    }/*from w ww  .ja  v  a2 s. c  o  m*/

    public static String getToday(int i) {
        SimpleDateFormat sdf = null;
        if (i == 1) {
            sdf = new SimpleDateFormat(fm);
        } else if (i == 2) {
            sdf = new SimpleDateFormat(fm2);
        } else if (i == 3) {
            sdf = new SimpleDateFormat(fm3);
        }
        Date dt = new Date();
        if (dt == null)
            return null;
        String result = sdf.format(dt);
        return result;
    }
}

Related Tutorials