/**
* <p> Common Date processing class </p>
* <p>description: General Date processing functions </p>
* <p>copyright:copyright (c) 2009-2099</p>
* <p>Company:fmcc,newland</p>
* @author Huang Yongjin
*/
Package mydate;
Import java.io.*;
Import Java.util.Date;
Import Java.util.Calendar;
Import Java.sql.Timestamp;
Import Java.text.DateFormat;
Import Java.text.SimpleDateFormat;
Import Java.util.Locale;
public class MyDate {
private int year;
private int month;
private int day;
private int hour;
private int minute;
private int second;
private static final int[] Dayarray = new int[]//number of days per month
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/**
* Standard date format mm/dd/yyyy
*/
public static final SimpleDateFormat date_format = new SimpleDateFormat (
"Mm/dd/yyyy");
/**
* Standard Time format mm/dd/yyyy hh:mm
*/
public static final SimpleDateFormat Date_time_format = new SimpleDateFormat (
"Mm/dd/yyyy hh:mm");
/**
* Standard Time format with time and seconds mm/dd/yyyy HH:mm:ss
*/
public static final SimpleDateFormat Date_time_extended_format = new SimpleDateFormat (
"Mm/dd/yyyy HH:mm:ss");
/**
* Ora standard date format YYYYMMDD
*/
public static final SimpleDateFormat Ora_date_format = new SimpleDateFormat (
"YyyyMMdd");
/**
* Ora Standard Time format yyyymmddhhmm
*/
public static final SimpleDateFormat Ora_date_time_format = new SimpleDateFormat (
"Yyyymmddhhmm");
/**
* Ora Standard Time format with time and seconds Yyyymmddhhmmss
*/
public static final SimpleDateFormat Ora_date_time_extended_format = new SimpleDateFormat (
"Yyyymmddhhmmss");
/**
* YYYY-MM-DD
*/
public static final SimpleDateFormat Chn_date_format = new SimpleDateFormat (
"Yyyy-mm-dd");
/**
* YYYY-MM-DD hh:mm
*/
public static final SimpleDateFormat Chn_date_time_format = new SimpleDateFormat (
"Yyyy-mm-dd hh:mm");
/**
* YYYY-MM-DD HH:MM:SS
*/
public static final SimpleDateFormat Chn_date_time_extended_format = new SimpleDateFormat (
"Yyyy-mm-dd HH:mm:ss");
/**
* Parameterless constructor, default date is System time
*/
MyDate () {
Today ();
}
/**
* Parameter Constructor
* @param invalue 14-bit date format string, less than 14 digits, followed by 0 processing
*/
MyDate (String invalue) {
Setdate (Invalue);
}
/**
* Parameter Constructor
* @param mills number of milliseconds
*/
MyDate (Long Mills) {
Settimeinmillis (Mills);
}
/**
* Parameter Constructor
* @param year
* @param month months
* @param Day
* When @param hour
* @param minute points
* @param second seconds
*/
mydate (int year,int month,int day,int hour,int minute,int second) {
Calendar calendar=calendar.getinstance ();
Calendar.set (year, month-1, day, hour, minute, second);
This.year = Calendar.get (calendar.year);
This.month = Calendar.get (calendar.month) +1;
This.day = Calendar.get (calendar.day_of_month);
This.hour = Calendar.get (Calendar.hour_of_day);
This.minute = Calendar.get (Calendar.minute);
This.second = Calendar.get (Calendar.second);
}
/**
* Set Date function
* @param invalue
*/
public void Setdate (String invalue) {
if (Invalue.length ()!=14) {//Not enough 14-bit date format, followed by 0 processing
for (int i=invalue.length (); i<14;i++)
{
Invalue = Invalue + "0";
}
System.out.println (Invalue);
}
try{
int year = Integer.parseint (invalue.substring (0, 4));
int month = Integer.parseint (invalue.substring (4, 6));
int day = Integer.parseint (invalue.substring (6, 8));
int hour = Integer.parseint (invalue.substring (8, 10));
int minute = Integer.parseint (invalue.substring (10, 12));
int second = Integer.parseint (invalue.substring (12));
Calendar calendar=calendar.getinstance ();
Calendar.set (year, month-1, day, hour, minute, second);
This.year = Calendar.get (calendar.year);
This.month = Calendar.get (calendar.month) +1;
This.day = Calendar.get (calendar.day_of_month);
This.hour = Calendar.get (Calendar.hour_of_day);
This.minute = Calendar.get (Calendar.minute);
This.second = Calendar.get (Calendar.second);
}catch (Exception e) {
System.out.println (E.getmessage ());
}
}
/**
* Get the current time
*/
private void Today () {
Calendar calendar=calendar.getinstance ();
This.year = Calendar.get (calendar.year);
This.month = Calendar.get (calendar.month) +1;
This.day = Calendar.get (calendar.day_of_month);
This.hour = Calendar.get (Calendar.hour_of_day);
This.minute = Calendar.get (Calendar.minute);
This.second = Calendar.get (Calendar.second);
}
/**
* Date of format
* @param SimpleDateFormat DF
* @return
*/
Public String Format (SimpleDateFormat DF) {
Date date = new Date (Year-1900,month-1,day,hour,minute,second);
return Df.format (date);
Calendar calendar=calendar.getinstance ();
Calendar.set (This.year, This.month-1, This.day, This.hour, This.minute, This.second);
Return Df.format (Calendar.gettime ());
}
/**
* Format date, it is not recommended to call this method directly outside
* Direct call can also be, just to ensure that the Strformat format to be correct
* @param Strformat
* @return
*/
public string Format2 (string strformat) {
/*date date = new Date (Year-1900,month-1,day,hour,minute,second);
SimpleDateFormat df = new SimpleDateFormat (Strformat);
return Df.format (date);
Calendar calendar=calendar.getinstance ();
SimpleDateFormat df = new SimpleDateFormat (Strformat);
Calendar.set (This.year, This.month-1, This.day, This.hour, This.minute, This.second);
Return Df.format (Calendar.gettime ());
}
/**
* ToString method with YYYY-MM-DD HH:mm:ss format
*/
Public String toString () {
Return This.format (this. Chn_date_time_extended_format);
}
public int getday () {
Return day;
}
public void Setday (int day) {
This.day = day;
}
public int Gethour () {
Return hour;
}
public void Sethour (int hour) {
This.hour = hour;
}
public int Getminute () {
return minute;
}
public void Setminute (int minute) {
This.minute = minute;
}
public int getmonth () {
Return month;
}
public void setmonth (int month) {
This.month = month;
}
public int Getsecond () {
return second;
}
public void Setsecond (int second) {
This.second = second;
}
public int getyear () {
return to year;
}
public void setyear (int year) {
This.year = year;
}
/**
* Get the number of milliseconds long integer
*/
Public long Gettimeinmillis () {
Calendar calendar=calendar.getinstance ();
Calendar.set (This.year, This.month-1, This.day, This.hour, This.minute, This.second);
Return Calendar.gettime (). GetTime ();
}
/**
* Set the date based on the number of milliseconds
* @param mills millisecond Long integer type
*/
public void Settimeinmillis (Long Mills) {
Date DD = new Date (mills);
Calendar calendar=calendar.getinstance ();
Calendar.settime (DD);
This.year = Calendar.get (calendar.year);
This.month = Calendar.get (calendar.month) +1;
This.day = Calendar.get (calendar.day_of_month);
This.hour = Calendar.get (Calendar.hour_of_day);
This.minute = Calendar.get (Calendar.minute);
This.second = Calendar.get (Calendar.second);
}
/**
* To determine whether the current year is leap years
* @return
*/
public Boolean isleapyear () {
Return This.isleapyear (year);
}
/**
* Judging whether it's a leap year
* @param year
* @return
*/
public boolean isleapyear (int year) {
If ((year% 400) = = 0)
return true;
else if ((year% 4) = = 0)
{
If ((year% 100) = = 0)
return false;
else return true;
}
else return false;
}
/**
* Date increase and decrease operation
* Number of years @param years
* Number of months @param months
* Number of days @param
* @param hours of hours
* @param minutes Score
* @param seconds seconds
*/
public void _add (int years,int months,int days,int hours,int minutes,int seconds) {
Calendar calendar=calendar.getinstance ();
Calendar.set (This.year+years, This.month-1+months, This.day+days, This.hour+hours, This.minute+minutes, This.second +seconds);
Settimeinmillis (Calendar.gettime (). GetTime ());
}
/**
* Annual increase or decrease operation
* Number of years @param years
*/
public void Addyear (int years) {
if (month = = 2 && day = 29)//Current time is leap year and date is February 29
{
if (this.isleapyear + years) = = true)//target year is leap years
This._add (years, 0, 0, 0, 0, 0);
Else This._add (years, 0,-1, 0, 0, 0);/The target year is not leap years, then February only 28 days, days minus 1
}
Else
This._add (years, 0, 0, 0, 0, 0);
}
/**
* Monthly increase or decrease operation (as with Oracle on the month increase or decrease operation)
* Number of months @param months
*/
public void addmonth (int months) {
int this_day_end = Daysofmonth ();//Days of the Month
int that_day_end = getdayofmonth (months);//days from now n months
if (This.day = = this_day_end) This.day = that_day_end;//If this is the last day of the month, then days is set to That_day_end
else if (This.day > That_day_end) this.day = That_day_end; If the number of days in the current N-month that_day_end is smaller than today's day, the day is now set to That_day_end
This._add (0, months, 0, 0, 0, 0);
}
/**
* Days increase or decrease operation
* @param days
*/
public void Addday (int days) {
This._add (0, 0, days, 0, 0, 0);
}
/**
* hours increase or decrease operation
* @param hours of hours
*/
public void Addhour (int hours) {
This._add (0, 0, 0, hours, 0, 0);
}
/**
* Minute increase or decrease operation
* @param minutes minutes
*/
public void Addminute (int minutes) {
This._add (0, 0, 0, 0, minutes, 0);
}
/**
* seconds increase or decrease operation
* @param seconds seconds
*/
public void Addsecond (int seconds) {
This._add (0, 0, 0, 0, 0, seconds);
}
/**
* How many days are there in this month?
* @param Month Month
* @return actual days of the month
*/
public int Daysofmonth () {
if (month>12 | | month<0) return 0;
if (month = 2 && this.isleapyear ())
return 29;
Else
return dayarray[month-1];
}
/**
* Get the number of days from Ms month of the current time
* @param MS Month
* @return
*/
public int GetDayOfMonth (int ms) {
int yy = MS/12;
int mm = MS%12;
int year = this.year + yy;
Int month = this.month + mm;
if (month>12)
{
month = month-12;
Year = year +1;
}
if (month<1)
{
month = month +12;
Year = year-1;
}
if (month = 2 && isleapyear (year))
return 29;
Else
return dayarray[month-1];
}
/**
* Get the number of seconds between two dates, static method
* Call Method: Mydate.diffsec (mydate1, Mydate2)
* To get the number of days between two dates, divide by (3600*24)
* @param mydate1
* @param mydate2
* @return
*/
public static long Diffsec (MyDate mydate1,mydate mydate2) {
Return (Mydate1.gettimeinmillis ()-mydate2.gettimeinmillis ())/1000;
}
/**********
* Time operations in Java are the only four scenarios:
* 1, get the current time
* 2, get some form of time
* 3, set time
* 4, the operation of the time
*
* Java calculation time depends on the number of milliseconds starting January 1, 1970.
*
* The Calender class itself is an abstract class, and some methods need to be abstract, so they cannot be instantiated by the new method.
* It needs to be instantiated with subclasses that have already implemented abstract methods, all of which are written in the getinstance () method
* When the universal implementation of an abstract method (instantiated using getinstance) does not meet your needs, you should implement it yourself. Like what:
* Public class Calenderex extends Calender {
* }
* Calender cal = new Calenderex ();
*
*
*/
}