问题描述
例如1900年1月1日 到 2010年11月18日 的总天数为 40498已知 40498如何计算出1900年1月1日后的40498天是2010年11月18日呢?注:用java来实现
解决方案
引用Java代码 public static void main(String[] args) { Calendar cal = Calendar.getInstance(); cal.set(1900, 0, 1); // Calendar cal2 = Calendar.getInstance(); // cal2.set(2010, 10, 18); // // long calm = cal.getTimeInMillis(); // long cal2m = cal2.getTimeInMillis(); // long re = (cal2m - calm)/1000/60/60/24; // System.out.println(re); cal.add(Calendar.DAY_OF_MONTH, 40498); System.out.println(cal.get(Calendar.YEAR)); System.out.println(cal.get(Calendar.MONTH)); System.out.println(cal.get(Calendar.DAY_OF_MONTH)); } LeeYee (中级程序员) 2012-03-16更正下,cal.get(Calendar.MONTH)定义的月是从0开始的,也就是0代表一月。public static void main(String[] args) {Calendar cal = Calendar.getInstance();cal.set(1900, 0, 1);cal.add(Calendar.DAY_OF_MONTH, 40498);System.out.print(cal.get(Calendar.YEAR) + "年");System.out.print(cal.get(Calendar.MONTH) + 1 + "月");System.out.println(cal.get(Calendar.DAY_OF_MONTH) + "日");}其实你可以直接查看Calendar API,API里有详细的说明
解决方案二:
import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import org.apache.commons.lang.time.DateUtils;public class TestDate {public static void main(String[] args){Calendar c = new GregorianCalendar(1900,0,1);Date d = c.getTime();System.out.println(d.toLocaleString());Date _d = DateUtils.addDays(d, 40498);System.out.println(_d.toLocaleString());}}
解决方案三:
public static void main(String[] args) {Calendar cal = Calendar.getInstance();cal.set(1900, 0, 1);// Calendar cal2 = Calendar.getInstance();// cal2.set(2010, 10, 18);//// long calm = cal.getTimeInMillis();// long cal2m = cal2.getTimeInMillis();// long re = (cal2m - calm)/1000/60/60/24;// System.out.println(re);cal.add(Calendar.DAY_OF_MONTH, 40498);System.out.println(cal.get(Calendar.YEAR));System.out.println(cal.get(Calendar.MONTH));System.out.println(cal.get(Calendar.DAY_OF_MONTH));}
解决方案四:
Calendar car = Calendar.getInstance(); car.set(Calendar.YEAR, 1900); car.set(Calendar.MONTH, 1); car.set(Calendar.DAY_OF_YEAR, 1); car.add(Calendar.DAY_OF_YEAR, 40498 ); System.out.println(car.getTime());