Conversion between a calendar and a calendar date (Java)

Source: Internet
Author: User

Package com. test;

Import java. Text. dateformat;
Import java. Text. simpledateformat;

Import java. util. calendar;
Import java. util. date;

Public class Test2 {
Private Final Static int month_days [] = {29, 30 };
Private Final Static int days_month [] [] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 31 },
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31 }};
Private Final Static int datas [] [] = {
{23, 3, 2, 17, 1, 0, 0, 1, 0, 0, 1, 1, 0 },
{41, 0, 4, 23, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1 },
{30, 7, 5, 28, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1 },
{49, 0, 6, 33, 1, 1, 0, 0, 1, 0, 0, 1 },
{38, 0, 0, 38, 1, 1, 0, 1, 0, 1, 0, 0, 1 },
{26, 6, 2, 44, 1, 1, 0, 1, 1, 0, 1, 0, 0 },
{45, 0, 3, 49, 1, 0, 1, 1, 0, 1, 0, 1, 0 },
{35, 0, 4, 54, 0, 1, 0, 1, 0, 1, 1, 0, 1 },
{24, 4, 5, 59, 1, 0, 1, 0, 1, 0, 1, 0, 1 },
{43, 0, 0, 5, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 },
{32, 0, 1, 10, 1, 0, 0, 1, 0, 0, 1, 0, 1 },
{21, 2, 2, 15, 1, 1, 0, 0, 1, 0, 0, 1 },
{40, 0, 3, 20, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 },
{28, 7, 5, 26, 1, 0, 1, 1, 0, 1, 0, 0, 1 },
{47, 0, 6, 31, 0, 1, 1, 0, 1, 1, 0, 0, 1 },
{36, 0, 0, 36, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0 },
{26, 5, 1, 41, 0, 1, 0, 1, 0, 1, 0, 1 },
{44, 0, 3, 47, 0, 1, 0, 0, 1, 1, 0, 1 },
{33, 0, 4, 52, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0 },
{23, 3, 5, 57,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0, 1, 0, 0, 0, 0, 1, 0 },
{43, 0, 0, 26, 1, 1, 0, 1, 0, 1, 0, 1 },
{32, 0, 1, 31, 1, 1, 0, 1, 1, 0, 1, 0, 0 },
{22, 3, 2, 36, 0, 1, 1, 0, 1, 0, 1, 0 }};

/**
* Obtain the corresponding calendar date based on the lunar calendar (the date must be between-years)
*
* @ Param date
* Lunar date (string)
* @ Return returns the corresponding calendar date.
* @ Throws exception
*/
Public static date getgregoriancalendar (string date ){
Calendar c = getcalendar (date );
Int year = C. Get (calendar. year );
Int month = C. Get (calendar. month) + 1;
Int day = C. Get (calendar. day_of_month );
Int Index = years-1936;
Int endindex = month;

If (datas [Index] [1]! = 0) & (month> datas [Index] [1]) {
Endindex ++;
}

Int daynum = 0;

For (INT I = 0; I <(endindex-1); I ++ ){
Daynum + = month_days [datas [Index] [4 + I];
}

Daynum + = Day;
Daynum + = datas [Index] [0];

Int year_days = 365;

If (isleapyear (year )){
Year_days = 366;
}

If (daynum> year_days ){
Year ++;
Daynum-= year_days;
}

Month = 1;

Int dayofmonth [] = days_month [0];

If (isleapyear (year )){
Dayofmonth = days_month [1];
}

Int I = 0;

For (; I <12; I ++ ){
Daynum-= dayofmonth [I];

If (daynum <= 0 ){
Break;
}

Month ++;
}

Day = dayofmonth [I] + daynum;

Return getdate (Year + "-" + month + "-" + Day );
}

/**
* Obtain the corresponding calendar date based on the Gregorian calendar (the date must be between-years)
*
* @ Param date
* Gregorian date (string)
* @ Return returns the lunar calendar date.
* @ Throws exception
*/
Public static date getlunarcalendar (string date ){
Calendar calendar = getcalendar (date );
Int year = calendar. Get (calendar. year );
Int month = 1;
Int Day;

If (year <1936) | (Year> 2028 )){
Return NULL;
}

Int Index = years-1936;
Int l_days = datas [Index] [0];
Int day_year = calendar. Get (calendar. day_of_year );
Int days;

If (day_year> = l_days ){
Days = day_year-l_days;
} Else {
Index --;
Year --;

Calendar c = getcalendar (Year + "-12-31 ");
Days = (C. Get (calendar. day_of_year) + day_year)-datas [Index] [0];
}

Int I = 0;
Int day_num = 0;

For (; I <13; I ++ ){
Day_num + = month_days [datas [Index] [I + 4];

If (day_num> = days ){
Break;
}

Month ++;
}

Day = month_days [datas [Index] [I + 4]-(day_num-days );

If (datas [Index] [1]! = 0) & (month> datas [Index] [1]) {
Month --;
}

Return getdate (Year + "-" + month + "-" + Day );
}

Private Static calendar getcalendar (string date ){
Date dd = getdate (date );
Calendar calendar = calendar. getinstance ();
Calendar. settime (dd );

Return calendar;
}

Private Static date getdate (string date ){
Dateformat format = new simpledateformat ("yyyy-mm-dd ");
Date dd = NULL;

Try {
Dd = format. parse (date );
} Catch (exception e ){
E. printstacktrace ();
}

Return dd;
}

Private Static Boolean isleapyear (INT year ){
If (Year % 400) = 0 ){
Return true;
} Else if (Year % 100) = 0 ){
Return false;
} Else if (Year % 4) = 0 ){
Return true;
}

Return false;
}
 
Public static void main (string [] ARGs ){
Simpledateformat dateformat = new simpledateformat ("yyyy-mm-dd ");
System. Out. println ("Lunar calendar:" + dateformat. Format (getlunarcalendar ("2010-08-03 ")));
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.