Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static final long ZERO_TIMESTAMP = 1044028800000L;
    private static final DateFormat FMT_DATE = new SimpleDateFormat("MM-dd");
    private static final DateFormat FMT_DATE2 = new SimpleDateFormat("yy-MM-dd");
    private static final DateFormat FMT_TIME = new SimpleDateFormat("HH:mm");

    public static String format(Date d) {
        if (d == null || d.getTime() < ZERO_TIMESTAMP)
            return "";
        Date now = new Date();
        if (now.getYear() == d.getYear() && now.getMonth() == d.getMonth() && now.getDate() == d.getDate())
            return FMT_TIME.format(d);
        else if (now.getYear() == d.getYear())
            return FMT_DATE.format(d);
        else
            return FMT_DATE2.format(d);
    }
}