Java Date Now getCurrentLastDate()

Here you can find the source of getCurrentLastDate()

Description

get Current Last Date

License

Open Source License

Declaration

public static String getCurrentLastDate() 

Method Source Code


//package com.java2s;
import java.text.*;
import java.util.*;

public class Main {
    private static String CurrentDate;

    public static String getCurrentLastDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = null;
        try {//from   w w  w.  jav  a  2s.c o m
            java.util.Date date = formatter.parse(getCurrentFirstDate());
            calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, 1);
            calendar.add(Calendar.DAY_OF_YEAR, -1);
            return formatDate(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getCurrentFirstDate() {
        Date NowDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-01");
        CurrentDate = formatter.format(NowDate);
        return CurrentDate;
    }

    public static String formatDate(java.util.Date date) {
        return formatDateByFormat(date, "yyyy-MM-dd");
    }

    public static int getTime(String sDateTime, String eDateTime) throws ParseException {
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date ssDateTime = myFormatter.parse(sDateTime);
        java.util.Date eeDateTime = myFormatter.parse(eDateTime);
        long l = (eeDateTime.getTime() - ssDateTime.getTime());
        long day = l / (24 * 60 * 60 * 1000);
        return (int) day;
    }

    public static String formatDateByFormat(java.util.Date date, String format) {
        String result = "";
        if (date != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                result = sdf.format(date);
            } catch (Exception ex) {

                ex.printStackTrace();
            }
        }
        return result;
    }
}

Related

  1. getCurrentFormattedTime()
  2. getCurrentGMTTime()
  3. getCurrentHMS()
  4. getCurrentISO8601Date()
  5. getCurrentJnlpTimestamp()
  6. getCurrentLocaleTime()
  7. getCurrentMinutes()
  8. getCurrentMonth()
  9. getCurrentMonth()