Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

import java.util.TimeZone;

public class Main {
    public static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String getDate(long time) {
        try {
            Calendar c = Calendar.getInstance();
            c.setTimeInMillis(time);
            return format.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getTime(long time) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);
        String times = "";
        if (minute < 10) {
            times = "0" + minute;
        } else {
            times = "" + minute;
        }
        if (second < 10) {
            times += ":0" + second;
        } else {
            times += ":" + second;
        }
        return times;
    }

    public static int[] getTime(long time, String timeZone) {
        Calendar c = Calendar.getInstance();
        if (timeZone != null && timeZone.length() != 0) {
            TimeZone tz = TimeZone.getTimeZone(timeZone);
            c = Calendar.getInstance(tz);
        }
        c.setTimeInMillis(time);
        int y = c.get(Calendar.YEAR);
        int m = c.get(Calendar.MONTH);
        int d = c.get(Calendar.DAY_OF_MONTH);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);
        return new int[] { y, m, d, hour, minute, second };
    }

    public static String getTime(String user_time) {
        String re_time = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d;
        try {

            d = sdf.parse(user_time);
            long l = d.getTime();

            String str = String.valueOf(l);
            // re_time = str.substring(0, 10);
            re_time = str;

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return re_time;
    }

    public static String getTime() {
        String re_time = null;
        long currentTime = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        Date d;
        d = new Date(currentTime);
        long l = d.getTime();
        String str = String.valueOf(l);
        re_time = str.substring(0, 10);
        return re_time;
    }
}