Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

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

public class Main {

    public static String getMonthStartDay(Date date, String dt_format) {
        if (date == null || dt_format == null) {
            return null;
        }
        Date startTime = getFirstDayOfMonth(date);
        return getTimeWithFormat(startTime, dt_format);
    }

    public static Date getFirstDayOfMonth(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance(Locale.getDefault());
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    public static String getTimeWithFormat(Date date, String format) {
        if (format == null) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
        if (date == null) {
            date = getDateTimeNow();
        }
        return sdf.format(date);
    }

    public static Date getDateTimeNow() {
        return new Date(getCurrMillis());
    }

    public static long getCurrMillis() {
        long _t = 0L;
        try {
            _t = Calendar.getInstance(Locale.getDefault()).getTimeInMillis();
            return _t;
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        _t = new Date().getTime();
        return _t;
    }
}