24. Gas Saving Algorithm

Source: Internet
Author: User
Tags idate

Package com. rytong. hnair;

Import java. util. calendar;
Import java. util. hashmap;
Import java. util. Map;

/**
* Describe: 24 solar terms
*
* Note: For information about the formula used in the program for calculating the solar term and the constant of the solar term century, see
*
* The majority of the results of program running are correct, and a few are incorrect.
*/
Public class solarterms {
Private Static final double D = 0.2422;
Private Final Static Map <string, integer []> increase_offsetmap = new hashmap <string, integer []> (); // + 1 offset
Private Final Static Map <string, integer []> decrease_offsetmap = new hashmap <string, integer []> (); //-1 offset

/** 24 solar terms **/
Private Static Enum solartermsenum {
Lichun, // -- Lichun
Yushui, // -- rain
Jingzhe, // -- stunned
Chunfen, // Spring Equinox
Qingming, // Pure Brightness
Guyu, // Gu Yu
Lixia, // lixia
Xiaoman, // Xiao man
Mangzhong, // Mountain
Xiazhi, // summer solstice
Xiaoshu, // slight heat
Dashu, // heat
Liqiu, // liqiu
Chushu, // heatmap
Bailu, // White Dew
Qiufen, // autumn score
Hanlu, // Cold Dew
Shuangjiang, // frost
Lidong, // Lidong
Xiaoxue, // Xiaoxue
DAXUE, // heavy snow
Dongzhi, // winter solstice
Xiaohan, // Xiaohan
Dahan; // Dahan
}

Static {
Decrease_offsetmap.put (solartermsenum. yushui. Name (), new integer [] {2026}); // rain
Increase_offsetmap.put (solartermsenum. chunfen. Name (), new integer [] {2084}); // Spring Equinox
Increase_offsetmap.put (solartermsenum. xiaoman. Name (), new integer [] {2008}); // fill
Increase_offsetmap.put (solartermsenum. mangzhong. Name (), new integer [] {1902}); // Mountain
Increase_offsetmap.put (solartermsenum. xiazhi. Name (), new integer [] {1928}); // summer solstice
Increase_offsetmap.put (solartermsenum. Xiaoshu. Name (), new integer [] {1925,201 6}); // slight heat
Increase_offsetmap.put (solartermsenum. dashu. Name (), new integer [] {1922}); // heat
Increase_offsetmap.put (solartermsenum. liqiu. Name (), new integer [] {2002}); // liqiu
Increase_offsetmap.put (solartermsenum. Bailu. Name (), new integer [] {1927}); // White Dew
Increase_offsetmap.put (solartermsenum. qiufen. Name (), new integer [] {1942}); // autumn score
Increase_offsetmap.put (solartermsenum. shuangjiang. Name (), new integer [] {2089}); // frost
Increase_offsetmap.put (solartermsenum. Lidong. Name (), new integer [] {2089}); // early winter
Increase_offsetmap.put (solartermsenum. Xiaoxue. Name (), new integer [] {1978}); // light snow
Increase_offsetmap.put (solartermsenum. DAXUE. Name (), new integer [] {1954}); // heavy snow
Decrease_offsetmap.put (solartermsenum. Dongzhi. Name (), new integer [] {1918,202 1}); // winter solstice

Increase_offsetmap.put (solartermsenum. Xiaohan. Name (), new integer [] {1982}); // small cold
Decrease_offsetmap.put (solartermsenum. Xiaohan. Name (), new integer [] {2019}); // small cold

Increase_offsetmap.put (solartermsenum. Dahan. Name (), new integer [] {2082}); // Dahan
}

// Define a two-dimensional array. The first-dimensional array stores the C value of the 20th century solar energy, and the second-dimensional array stores the C value of the 21st century solar energy, ranging from 0 to 23, represent spring, rain in turn... C value of big cold and solar energy
Private Static final double [] [] century_array = {
{4.6295, 19.4599, 6.3826, 21.4155, 5.59, 20.888, 6.318, 21.86, 6.5, 22.2, 7.928, 23.65, 8.35, 23.95, 8.44,
23.822, 9.098, 24.218, 8.218, 23.08, 7.9, 22.6, 6.11, 20.84 },
{3.87, 18.73, 5.63, 20.646, 4.81, 20.1, 5.52, 21.04, 5.678, 21.37, 7.108, 22.83, 7.5, 23.13, 7.646,
23.042, 8.318, 23.438, 7.438, 22.36, 7.18, 21.94, 5.4055, 20.12 }};

/**
*
* @ Param year
* Year
* @ Param name
* Term name
* @ Return returns the day of the corresponding month.
*/
Public static int getsolartermnum (INT year, string name ){

Double centuryvalue = 0; // The century value of the solar term. Each century value of the solar term is different.
Name = Name. Trim (). touppercase ();
Int ordinal = solartermsenum. valueof (name). ordinal ();

Int centuryindex =-1;
If (Year> = 1901 & year <= 2000) {// 20th Century
Centuryindex = 0;
} Else if (Year> = 2001 & year <= 2100) {// 21st Century
Centuryindex = 1;
} Else {
Throw new runtimeexception ("this year is not supported:" + year + ", currently only the time range from January 1, 1901 to January 1, 2100 ");
}
Centuryvalue = century_array [centuryindex] [ordinal];
Int datenum = 0;
/**
* Calculate num = [y * D + C]-l this is a general formula of the legendary life star. Interpretation: after the second digit of the number of years is multiplied by 0.2422 plus C (I .e. centuryvalue, leap year reduction
*/
Int y = year % 100; // Step 1: Take the last two digits of the year
If (Year % 4 = 0 & amp; Year % 100! = 0 | year % 400 = 0) {// leap year
If (ordinal = solartermsenum. Xiaohan. ordinal () | ordinal = solartermsenum. Dahan. ordinal ()
| Ordinal = solartermsenum. Lichun. ordinal () | ordinal = solartermsenum. yushui. ordinal ()){
// Note: Any leap year before March 1 leap years to reduce one, namely: L = [(Y-1)/4], because small cold, big cold, spring, rain these two solar terms are less than March 1, so y = Y-1
Y = Y-1; // Step 2
}
}
Datenum = (INT) (y * D + centuryvalue)-(INT) (y/4); // step 3, use the formula [y * D + C]-l for Calculation
Datenum + = specialyearoffset (year, name); // Step 4, with the special year offset
Return datenum;
}

/**
* In special cases, due to the imperfect formula, the number of days of a certain solar term is not accurate, and the offset is returned here.
*
* @ Param year
* Year
* @ Param name
* Term name
* @ Return returns its offset.
*/
Public static int specialyearoffset (INT year, string name ){
Int offset = 0;
Offset + = getoffset (decrease_offsetmap, year, name,-1 );
Offset + = getoffset (increase_offsetmap, year, name, 1 );

Return offset;
}

Public static int getoffset (Map <string, integer []> map, int year, string name, int offset ){
Int off = 0;
Integer [] years = map. Get (name );
If (null! = Years ){
For (int I: years ){
If (I = year ){
Off = offset;
Break;
}
}
}
Return off;
}

Public static string solartermtostring (INT year ){
Stringbuffer sb = new stringbuffer ();
// Sb. append ("---"). append (year );
// If (Year % 4 = 0 & year % 100! = 0 | year % 400 = 0) {// leap year
// Sb. append (" ");
//} Else {
// Sb. append (" ");
//}

// Sb. append ("\ n "). append ("Spring: February "). append (getsolartermnum (year, solartermsenum. lichun. name ())). append ("day, rain: February ")
//. Append (getsolartermnum (year, solartermsenum. yushui. Name (). append ("day, shocking: March"). append (
// Getsolartermnum (year, solartermsenum. Jingzhe. Name (). append ("day, Spring Equinox: March"). append (
// Getsolartermnum (year, solartermsenum. chunfen. Name (). append ("day,: April"). append (
// Getsolartermnum (year, solartermsenum. Qingming. Name (). append ("day, grain rain: April"). append (
// Getsolartermnum (year, solartermsenum. Guyu. Name (). append ("day, summer: May"). append (
// Getsolartermnum (year, solartermsenum. lixia. Name (). append ("day, fill: May"). append (
// Getsolartermnum (year, solartermsenum. xiaoman. Name (). append ("day, mountain: June"). append (
// Getsolartermnum (year, solartermsenum. mangzhong. Name (). append ("day, Summer Solstice: June"). append (
// Getsolartermnum (year, solartermsenum. xiazhi. Name (). append ("day, summer heat: July"). append (
// Getsolartermnum (year, solartermsenum. Xiaoshu. Name (). append ("day, heat: July"). append (
// Getsolartermnum (year, solartermsenum. dashu. Name (). append ("day, \ n: August"). append (
// Getsolartermnum (year, solartermsenum. liqiu. Name (). append ("day, heat: August"). append (
// Getsolartermnum (year, solartermsenum. chushu. Name (). append ("day, white dew: September"). append (
// Getsolartermnum (year, solartermsenum. Bailu. Name (). append ("day, autumn: September"). append (
// Getsolartermnum (year, solartermsenum. qiufen. Name (). append ("day, Cold Dew: October"). append (
// Getsolartermnum (year, solartermsenum. hanlu. Name (). append ("day, Frost: October"). append (
// Getsolartermnum (year, solartermsenum. shuangjiang. Name (). append ("day, Winter: November"). append (
// Getsolartermnum (year, solartermsenum. Lidong. Name (). append ("day, snow: November"). append (
// Getsolartermnum (year, solartermsenum. Xiaoxue. Name (). append ("day, heavy snow: December"). append (
// Getsolartermnum (year, solartermsenum. DAXUE. Name (). append ("day, Winter Solstice: December"). append (
// Getsolartermnum (year, solartermsenum. Dongzhi. Name (). append ("day, cold: January"). append (
// Getsolartermnum (year, solartermsenum. Xiaohan. Name (). append ("day, big cold: January"). append (
// Getsolartermnum (year, solartermsenum. Dahan. Name ()));
SB. append ("2:"). append (getsolartermnum (year, solartermsenum. Lichun. Name (). append (", 2:"). append (
Getsolartermnum (year, solartermsenum. yushui. Name (). append (", 3:"). append (
Getsolartermnum (year, solartermsenum. Jingzhe. Name (). append (", 3:"). append (
Getsolartermnum (year, solartermsenum. chunfen. Name (). append (", 4:"). append (
Getsolartermnum (year, solartermsenum. Qingming. Name (). append (", 4:"). append (
Getsolartermnum (year, solartermsenum. Guyu. Name (). append (", 5:"). append (
Getsolartermnum (year, solartermsenum. lixia. Name (). append (", 5:"). append (
Getsolartermnum (year, solartermsenum. xiaoman. Name (). append (", 6:"). append (
Getsolartermnum (year, solartermsenum. mangzhong. Name (). append (", 6:"). append (
Getsolartermnum (year, solartermsenum. xiazhi. Name (). append (", 7:"). append (
Getsolartermnum (year, solartermsenum. Xiaoshu. Name (). append (", 7:"). append (
Getsolartermnum (year, solartermsenum. dashu. Name (). append (", 8:"). append (
Getsolartermnum (year, solartermsenum. liqiu. Name (). append (", 8:"). append (
Getsolartermnum (year, solartermsenum. chushu. Name (). append (", 9:"). append (
Getsolartermnum (year, solartermsenum. Bailu. Name (). append (", 9:"). append (
Getsolartermnum (year, solartermsenum. qiufen. Name (). append (", 10:"). append (
Getsolartermnum (year, solartermsenum. hanlu. Name (). append (", 10:"). append (
Getsolartermnum (year, solartermsenum. shuangjiang. Name (). append (", 11:"). append (
Getsolartermnum (year, solartermsenum. Lidong. Name (). append (", 11:"). append (
Getsolartermnum (year, solartermsenum. Xiaoxue. Name (). append (", 12:"). append (
Getsolartermnum (year, solartermsenum. DAXUE. Name (). append (", 12:"). append (
Getsolartermnum (year, solartermsenum. Dongzhi. Name (). append (", 1:"). append (
Getsolartermnum (year, solartermsenum. Xiaohan. Name (). append (", 1:"). append (
Getsolartermnum (year, solartermsenum. Dahan. Name ()));

Return sb. tostring ();
}

Public static int [] [] gensolartermsdays (){
Calendar Cal = calendar. getinstance ();
Int year = Cal. Get (calendar. year );
Return linear2twodimensional (solartermtostring (year). Split (","));
}

/**
* Convert an array into a two-dimensional array
*
* @ Param arr
* @ Return
*/
Public static int [] [] linear2twodimensional (string [] ARR ){
Int s [] [] = new int [arr. Length] []; // declare a two-dimensional array
For (INT I = 0; I <arr. length; I ++ ){
String [] sdate = arr [I]. Split (":");
Int [] idate = new int [sdate. Length];
For (Int J = 0; j <sdate. length; j ++ ){
Idate [J] = integer. valueof (sdate [J]. Trim ());
}
S [I] = idate;

}
Return S;
}

}

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.