Java gets a summary of the implementation method for the specified date

  • 2020-04-01 02:11:10
  • OfStack

 Format date  String-->Date  or  Data-->String

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
Date date = sdf.parse("2009-11-04");//String-->Date  

String sdate = sdf.format(date );// Data-->String ===============================================================
package com.hefeng.test;
import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class TimeTest {
//It is used to control the number of weeks in the previous week, this week and the next week
private int weeks = 0;
private int MaxDate;//Maximum days in January
private int MaxYear;//Maximum days of the year

public static void main(String[] args) {
TimeTest tt = new TimeTest();

System.out.println(" get 6 Month after the date :"+tt .getAfterMonth(6);
System.out.println(" Get the date of the day :"+tt.getNowTime("yyyy-MM-dd"));
System.out.println(" Get Monday date :"+tt.getMondayOFWeek());
System.out.println(" Get the date of this Sunday ~:"+tt.getCurrentWeekday());
System.out.println(" Get last Monday's date :"+tt.getPreviousWeekday());
System.out.println(" Get last Sunday date :"+tt.getPreviousWeekSunday());
System.out.println(" Get next Monday's date :"+tt.getNextMonday());
System.out.println(" Get next Sunday's date :"+tt.getNextSunday());
System.out.println(" Get the Saturday of the corresponding week :"+tt.getNowTime("yyyy-MM-dd"));
System.out.println(" Get the first day of the month date :"+tt.getFirstDayOfMonth());
System.out.println(" Get the last day of the month date :"+tt.getDefaultDay());
System.out.println(" Get the first day date of last month :"+tt.getPreviousMonthFirst());
System.out.println(" Get the date of the last day of the previous month :"+tt.getPreviousMonthEnd());
System.out.println(" Get the first day of the next month :"+tt.getNextMonthFirst());
System.out.println(" Get the last day of the next month :"+tt.getNextMonthEnd());
System.out.println(" Get the first day of the year :"+tt.getCurrentYearFirst());
System.out.println(" Gets the last day of the year :"+tt.getCurrentYearEnd());
System.out.println(" Get last year's first day date :"+tt.getPreviousYearFirst());
System.out.println(" Get last year's last day date :"+tt.getPreviousYearEnd());
System.out.println(" Get the first day date of the next year :"+tt.getNextYearFirst());
System.out.println(" Get last day date for next year :"+tt.getNextYearEnd());
System.out.println(" Get the first to last day of the season :"+tt.getThisSeasonTime(11));
System.out.println(" Gets the number of days between the two dates 2008-12-1~2008-
.29:"+TimeTest.getTwoDay("2008-12-1","2008-9-29"));
}



public static String getAfterMonth(int month) {
    Calendar c = Calendar.getInstance();//Gets an instance of a calendar
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
    Date date = null;  
    try{  
      date = sdf.parse("2009-11-04");//The initial date
    }catch(Exception e){ 

    }  
    c.setTime(date);//Set calendar time
    c.add(Calendar.MONTH,month);//Add 6 months to the calendar month
    String strDate = sdf.format(c.getTime()));//By the date you want in 6 months
    return strDate;

 }



public static String getTwoDay(String sj1, String sj2) {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
long day = 0;
try {
java.util.Date date = myFormatter.parse(sj1);
java.util.Date mydate = myFormatter.parse(sj2);
day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
} catch (Exception e) {
return "";
}
return day + "";
}

public static String getWeek(String sdate) {
//And then convert to time
Date date = TimeTest.strToDate(sdate);
Calendar c = Calendar.getInstance();
c.setTime(date);
// int hour=c.get(Calendar.DAY_OF_WEEK);
//The day of the week is stored in "hour", which ranges from 1 to 7
//1= Sunday 7= Saturday, and so on
return new SimpleDateFormat("EEEE").format(c.getTime());
}

public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}

public static long getDays(String date1, String date2) {
if (date1 == null || date1.equals(""))
return 0;
if (date2 == null || date2.equals(""))
return 0;
//Convert to standard time
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = null;
java.util.Date mydate = null;
try {
date = myFormatter.parse(date1);
mydate = myFormatter.parse(date2);
} catch (Exception e) {
}
long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
return day;
}
//Calculates the last day of the month and returns a string
public String getDefaultDay(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.set(Calendar.DATE,1);//Set it to the 1st of the current month
lastDate.add(Calendar.MONTH,1);//Add one month to make it the 1st of next month
lastDate.add(Calendar.DATE,-1);//Minus one day, becomes the last day of the month
str=sdf.format(lastDate.getTime());
return str;
}
//First day of last month
public String getPreviousMonthFirst(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.set(Calendar.DATE,1);//Set it to the 1st of the current month
lastDate.add(Calendar.MONTH,-1);//Minus one month, to the first of next month
//lastDate.add(Calendar.DATE,-1);//Minus one day, becomes the last day of the month
str=sdf.format(lastDate.getTime());
return str;
}
//Gets the first day of the month
public String getFirstDayOfMonth(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.set(Calendar.DATE,1);//Set it to the 1st of the current month
str=sdf.format(lastDate.getTime());
return str;
}
//Get the date of Sunday this week
public String getCurrentWeekday() {
weeks = 0;
int mondayPlus = this.getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus+6);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}
//Get the day
public String getNowTime(String dateformat){
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);//Can be
 To facilitate 
 Modify the date format 
String hehe = dateFormat.format(now);
return hehe;
}
//Get the number of days between the current date and this Sunday
private int getMondayPlus() {
Calendar cd = Calendar.getInstance();
//Today is the day of the week, Sunday is the first day, Tuesday is the second day...
int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK)-1; //Because it's Monday in China
 As the first 
 Day so minus here 1
if (dayOfWeek == 1) {
return 0;
} else {
return 1 - dayOfWeek;
}
}
//Get the date for Monday
public String getMondayOFWeek(){
weeks = 0;
int mondayPlus = this.getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}
//Get the date of the Saturday of the corresponding week
public String getSaturday() {
int mondayPlus = this.getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks + 6);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}
//Get the date of last Sunday
public String getPreviousWeekSunday() {
weeks=0;
weeks--;
int mondayPlus = this.getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus+weeks);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}
//Get the date of last Monday
public String getPreviousWeekday() {
weeks--;
int mondayPlus = this.getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}
//Get the date of next Monday
public String getNextMonday() {
weeks++;
int mondayPlus = this.getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus + 7);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}
//Get the date of next Sunday
public String getNextSunday() {
int mondayPlus = this.getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus + 7+6);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}
private int getMonthPlus(){
Calendar cd = Calendar.getInstance();
int monthOfNumber = cd.get(Calendar.DAY_OF_MONTH);
cd.set(Calendar.DATE, 1);//Set the date to the first day of the month
cd.roll(Calendar.DATE, -1);//The date rolls back one day, the last day
MaxDate=cd.get(Calendar.DATE);
if(monthOfNumber == 1){
return -MaxDate;
}else{
return 1-monthOfNumber;
}
}
//Get the date of the last day of the previous month
public String getPreviousMonthEnd(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.add(Calendar.MONTH,-1);//Minus one month
lastDate.set(Calendar.DATE, 1);//Set the date to the first day of the month
lastDate.roll(Calendar.DATE, -1);//The date rolls back one day, the last day of the month
str=sdf.format(lastDate.getTime());
return str;
}
//Get the date of the first day of next month
public String getNextMonthFirst(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.add(Calendar.MONTH,1);//Minus one month
lastDate.set(Calendar.DATE, 1);//Set the date to the first day of the month
str=sdf.format(lastDate.getTime());
return str;
}
//Get the date of the last day of next month
public String getNextMonthEnd(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.add(Calendar.MONTH,1);//Add a month
lastDate.set(Calendar.DATE, 1);//Set the date to the first day of the month
lastDate.roll(Calendar.DATE, -1);//The date rolls back one day, the last day of the month
str=sdf.format(lastDate.getTime());
return str;
}
//Get the date of the last day of next year
public String getNextYearEnd(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.add(Calendar.YEAR,1);//Add in a
lastDate.set(Calendar.DAY_OF_YEAR, 1);
lastDate.roll(Calendar.DAY_OF_YEAR, -1);
str=sdf.format(lastDate.getTime());
return str;
}
//Get the date of the first day of the next year
public String getNextYearFirst(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.add(Calendar.YEAR,1);//Add in a
lastDate.set(Calendar.DAY_OF_YEAR, 1);
str=sdf.format(lastDate.getTime());
return str;
}
//Get the number of days in the year
private int getMaxYear(){
Calendar cd = Calendar.getInstance();
cd.set(Calendar.DAY_OF_YEAR,1);//Set the date to the first day of the year
cd.roll(Calendar.DAY_OF_YEAR,-1);//Roll the date back one day.
int MaxYear = cd.get(Calendar.DAY_OF_YEAR);
return MaxYear;
}
private int getYearPlus(){
Calendar cd = Calendar.getInstance();
int yearOfNumber = cd.get(Calendar.DAY_OF_YEAR);//The day of acquisition is the day of the year
cd.set(Calendar.DAY_OF_YEAR,1);//Set the date to the first day of the year
cd.roll(Calendar.DAY_OF_YEAR,-1);//Roll the date back one day.
int MaxYear = cd.get(Calendar.DAY_OF_YEAR);
if(yearOfNumber == 1){
return -MaxYear;
}else{
return 1-yearOfNumber;
}
}
//Obtain the date of the first day of the current year
public String getCurrentYearFirst(){
int yearPlus = this.getYearPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE,yearPlus);
Date yearDay = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preYearDay = df.format(yearDay);
return preYearDay;
}
//Obtain the date of the last day of the current year *
public String getCurrentYearEnd(){
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//Can be Israel 
 Fix for it 
 Change date format 
String years = dateFormat.format(date);
return years+"-12-31";
}

//Get the date of the first day of the previous year *
public String getPreviousYearFirst(){
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//Can be Israel 
 Fix for it 
 Change date format 
String years = dateFormat.format(date); int years_value = Integer.parseInt
(years);
years_value--;
return years_value+"-1-1";
}
//Get the date of the last day of the previous year
public String getPreviousYearEnd(){
weeks--;
int yearPlus = this.getYearPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE,yearPlus+MaxYear*weeks+(MaxYear-1));
Date yearDay = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preYearDay = df.format(yearDay);
getThisSeasonTime(11);
return preYearDay;
}
//Get the quarter
public String getThisSeasonTime(int month){
int array[][] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
int season = 1;
if(month>=1&&month<=3){
season = 1;
}
if(month>=4&&month<=6){
season = 2;
}
if(month>=7&&month<=9){
season = 3;
}
if(month>=10&&month<=12){
season = 4;
}
int start_month = array[season-1][0];
int end_month = array[season-1][2];
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//Can be Israel 
 Fix for it 
 Change date format 
String years = dateFormat.format(date);
int years_value = Integer.parseInt(years);
int start_days =1;//years+"-"+String.valueOf(start_month)+"-
";//getLastDayOfMonth(years_value,start_month);
int end_days = getLastDayOfMonth(years_value,end_month);
String seasonDate =
years_value+"-"+start_month+"-"+start_days+";"+years_value+"-"+end_month+"-"+end_days;
return seasonDate;
}

private int getLastDayOfMonth(int year, int month) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12) {
return 31;
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
}
if (month == 2) {
if (isLeapYear(year)) {
return 29;
} else {
return 28;
}
}
return 0;
}

public boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

Related articles: