java中陰曆和陽曆的轉換
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.text.DateFormat; /** * 陰陽曆自動轉換工具函式 * @author admin * @date 2013-8-23上午11:12:50 */ public class Lunar { private final static int MONTH_DAYS[] = { 29, 30 }; private final static int DAYS_MONTH[][] = { { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } }; private final static int DATAS[][] = { { 23, 3, 2, 17, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 }, { 41, 0, 4, 23, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 }, { 30, 7, 5, 28, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 }, { 49, 0, 6, 33, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 }, { 38, 0, 0, 38, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 }, { 26, 6, 2, 44, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 }, { 45, 0, 3, 49, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 }, { 35, 0, 4, 54, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 }, { 24, 4, 5, 59, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 }, { 43, 0, 0, 5, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 }, { 32, 0, 1, 10, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1 }, { 21, 2, 2, 15, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 }, { 40, 0, 3, 20, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 }, { 28, 7, 5, 26, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 }, { 47, 0, 6, 31, 0, 1, 1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 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1 }, { 36, 0, 4, 15, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 }, { 25, 5, 5, 20, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 }, { 43, 0, 0, 26, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 }, { 32, 0, 1, 31, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0 }, { 22, 3, 2, 36, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 } }; private static String dateToStr(Date d, int type) { String s = ""; SimpleDateFormat sdf = null; switch (type) { case 1: sdf = new SimpleDateFormat("yyyy-MM-dd"); case 2: sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); case 3: sdf = new SimpleDateFormat("yyyyMMddHHmmss"); } s = sdf.format(d); return s; } /** * 根據陰曆取得對應的陽曆日期(該日期必須在1936—2028年之間) * * @param date 陰曆日期(字串) * @return 返回對應的陽曆日期 * @throws Exception */ public String lunarTosolar(String date) { Date d = getGregorianCalendar(date); String s = dateToStr(d, 1); s = s.substring(0, 4) + "-" + s.substring(4, 6) + "-" + s.substring(6, 8); return s; } /** * 根據陽曆取得對應的陰曆日期(該日期必須在1936—2028年之間) * * @param date 陽曆日期(字串) * @return 返回陰曆日期 * @throws Exception */ public String solarTolunar(String date) { Date d = getLunarCalendar(date); String s = dateToStr(d, 1); s = s.substring(0, 4) + "-" + s.substring(4, 6) + "-" + s.substring(6, 8); return s; } private 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 = year - 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); } private 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 = year - 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){ Lunar lunar = new Lunar(); DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); String nowTime = format.format(date.getTime()); System.out.println(nowTime); System.out.println(lunar.solarTolunar(nowTime)); System.out.println(lunar.lunarTosolar(nowTime)); } }
相關推薦
java中陰曆和陽曆的轉換
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.text.DateFormat; /** * 陰陽曆自動轉換工具函
Java中float和double轉換的問題
leg throw exceptio int row 如何 方法 避免 stat 為什麽double轉float不會出現數據誤差,而float轉double卻誤差如此之大? double d = 3.14; float f = (float)d; Syst
java中string和int的相互轉換
integer chan 也會 tee tex 什麽 產生 chang details java中string和int的相互轉換 int -> String int i=12345;String s="";核心:s=i+""; String -> int s="
java中List和Array相互轉換
一個 data element 分隔 -a common 內部類 iter lang List to Array List 提供了toArray的接口,所以可以直接調用轉為object型數組 List<String> list = new ArrayLis
c#中的裏氏轉換和Java中強制類型轉換在多態中的應用
readline color extends pre pri console AS 定義 spa 在c#中: 註意: 子類並沒有繼承父類的構造函數,而是會默認調用父類那個無參數的構造函數。 如果一個子類繼承了一個父類,那麽這個子類除了可以使用自己的成員外,還可以使用從父類
Java中物件和json互相轉換的工具類
package com.Dingyu.util; import java.util.List; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JavaT
java中map和表單字串相互轉換
一、需求 serialnum=123456&data=357c0a04f&enable=true&key=b5b806d0dc9 帶有&分割的字串,轉化為map的key、value型別儲存,反之也行。 二、程式碼 import java.util.
Java中字串和byte陣列之間的相互轉換
1、將字元轉換成byte陣列 String str = "羅長"; byte[] sb = str.getBytes(); 2、將byte陣列轉換成字元 byte[] b={(byte)0xB8,(byte)0xDF,(byte)0xCB,(byte)0xD9}; String str=
Java中String和byte[]間的轉換淺析
Java語言中字串型別和位元組陣列型別相互之間的轉換經常發生,網上的分析及程式碼也比較多,本文將分析總結常規的byte[]和String間的轉換以及十六進位制String和byte[]間相互轉換的原理及實現。 1. String轉byte[] 首先我們來分析一下常規的Strin
Java中String和Int類相互轉換的方法
String Sch_No = "1707020410"; int output = Integer.parseInt(Sch_No); 這樣就將學號“1707020410” 轉換成了純數字1707020410 其他的也是類似的 double d = Double.p
java中arraylist和一維陣列,二維陣列的轉換
package com.xunfang.epay.util; import java.util.ArrayList; public class TwoArray { // 陣列轉換問題 public static void main(String[] args)
關於JAVA中:int和byte的互相轉換
一、基礎概念: 1.原碼: 一個byte是一個位元組,一個位元組是由8個位組成。其中最高位是符號位,範圍就是127 ~ -128。 即:0111 1111~1111 1111 也就是說:0000 0001 代表的是1, 1000 0000 代表的是-128
Java中字元和byte陣列之間的相互轉換
Java與其他語言編寫的程式進行tcp/ip socket通訊時,通訊內容一般都轉換成byte陣列型,java在字元與陣列轉換也是非常方便的; 1、將字元轉換成byte陣列 String str = "羅長"; byte[] sb = str.getBy
Java中char和String的轉換
Java中char是一個基本型別,而String是一個引用型別。有時候我們需要在它們之間互相轉換。 String轉換為char 在Java中將String轉換為char是非常簡單的。 1. 使
Java中int和Integer相互轉換
Java是一種面嚮物件語言,為了能將基本型別視為物件來處理,並能連線相關的方法,Java為每個基本資料型別都提供了包裝類,這樣便可以把這些基本型別轉化為物件來處理。 基本資料型別及對應的包裝類 基本資料型別 包裝類 基本資料型別 包裝類 boolean Boo
java 中 image 和 byte[] 相互轉換
只需要一個儲存了圖片資訊的二進位制串(byte[])然後,這樣:InputStream buffin = new ByteArrayInputStream(/*二進位制串*/, /*起始位置*/,/*二進位制串長度*/)); BufferedImage img = Image
java中陣列和容器的轉換
可以用Arrays.asList( )方法把陣列改造成List:實際上它只是部分地實現了Collection 和List 介面。呼叫其它方法會引發一個UnsupportedOperationException 異常。比如你不能用add方法.Arrays.asList( )返回
js和java中日期和日期字串的相互轉換和使用
摘要:在工作中只要牽扯到日期,很大可能都會牽扯到日期的計算格式的轉換等用法,這篇文章就是要探討一下平常在 js和java中對於日期的使用。 js中日期的使用 js中日期的計算 和 比較 js中兩個日期字串的計算 ###########
Java中的向上型別轉換和向下型別轉換
一、向上轉型。 通俗地講即是將子類物件轉為父類物件。此處父類物件可以是介面。 1,向上轉型中的方法呼叫。 看下面程式碼: package com.wensefu.others; publicclass Animal { publicv
java中char和int型別的相互轉換
char型別轉int型: char numChar = '9'; int nSum = numChar-'0'; int型別轉cahr型:int num = 9; char numChar = n