Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.Context;
import android.text.TextUtils;
import android.text.format.DateUtils;

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

public class Main {

    public static String formatDateTime(Context context, long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date d = new Date(time);
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        int year = Integer.parseInt(TextUtils.isDigitsOnly(sdf.format(d)) ? sdf.format(d) : currentYear + "");
        if (currentYear == year) {
            return DateUtils.formatDateTime(context, time, DateUtils.FORMAT_SHOW_DATE
                    //| DateUtils.FORMAT_SHOW_WEEKDAY
                    //| DateUtils.FORMAT_SHOW_YEAR
                    | DateUtils.FORMAT_ABBREV_MONTH
                    //| DateUtils.FORMAT_ABBREV_WEEKDAY
                    | DateUtils.FORMAT_ABBREV_TIME | DateUtils.FORMAT_SHOW_TIME);
        } else {
            return DateUtils.formatDateTime(context, time, DateUtils.FORMAT_SHOW_DATE
                    //| DateUtils.FORMAT_SHOW_WEEKDAY
                    | DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_ABBREV_MONTH
                    //| DateUtils.FORMAT_ABBREV_WEEKDAY
                    | DateUtils.FORMAT_ABBREV_TIME | DateUtils.FORMAT_SHOW_TIME);
        }

    }
}