假设 1 年等于 java 中的 360 天,如何将 n 天显示为日期?
how to display n day as date assuming 1 year equals 360 days in java?
这是我的代码,
int txtSubsDay = Integer.parseInt(this.textSubDay.getText());
int txtDay = Integer.parseInt(this.textDay.getText());
int txtMonth = Integer.parseInt(this.textMonth.getText());
int txtYear = Integer.parseInt(this.textYear.getText());
int daysToSubs = txtSubsDay;
int convertedYears = (int)(daysToSubs / 360);
int convertedMonths = (int)((daysToSubs % 360) / 30);
int convertedDays = (int)((daysToSubs % 360) % 30);
Calendar calendar = new GregorianCalendar(txtYear, txtMonth-1, txtDay);
calendar.add(Calendar.MONTH, -convertedMonths);
calendar.add(Calendar.DAY_OF_MONTH, -convertedDays);
calendar.add(Calendar.YEAR, -convertedYears);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MM yyyy");
SimpleDateFormat dayFormat = new SimpleDateFormat("d MMM yyyy");
String lastDate = dateFormat.format(calendar.getTime());
String lastDate2 = dayFormat.format(calendar.getTime());
labelOutput.setText(lastDate);
labelOutput2.setText(lastDate2);
这是我想要的。//////////////////////////////////// /
它节省了我的时间。
您需要计算两个日期之间的天数
//we have two dates, now and day 0
Date d1 = new Date(0);
Date d2 = new Date();
//substract times from each other, and divide this by number milisecond in day
int days =(int)((d2.getTime()-d1.getTime()) /(1000*60*60 *24) );
//and display your result
System.out.println(days+" days = "+(days/360)+" years "+((days%360)/30)+" month "+days%30);
int totalDays = 548;
int years = (int)(totalDays / 360);
int months = (int((totalDays % 360) / 30);
int days = (int)((totalDays % 360) % 30);
System.out.println("years: " + years + "; months: " + months + "; days: " + days);
// Result: years: 1; months: 6; days: 8
还是不太明白你为什么坚持用360年,用30几个月。如果您只想从一个日期中减去天数以获得过去的正确日期,请改用:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date dateInstance = dateFormat.parse("27/07/2015");
int daysSubstracted = 548;
Calendar cal = Calendar.getInstance();
cal.setTime(dateInstance);
cal.add(Calendar.DATE, -daysSubstracted);
Date dateInPast = cal.getTime();
System.out.println(dateFormat.format(dateInPast));
// Result: 25/01/2014
这是我的代码,
int txtSubsDay = Integer.parseInt(this.textSubDay.getText());
int txtDay = Integer.parseInt(this.textDay.getText());
int txtMonth = Integer.parseInt(this.textMonth.getText());
int txtYear = Integer.parseInt(this.textYear.getText());
int daysToSubs = txtSubsDay;
int convertedYears = (int)(daysToSubs / 360);
int convertedMonths = (int)((daysToSubs % 360) / 30);
int convertedDays = (int)((daysToSubs % 360) % 30);
Calendar calendar = new GregorianCalendar(txtYear, txtMonth-1, txtDay);
calendar.add(Calendar.MONTH, -convertedMonths);
calendar.add(Calendar.DAY_OF_MONTH, -convertedDays);
calendar.add(Calendar.YEAR, -convertedYears);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MM yyyy");
SimpleDateFormat dayFormat = new SimpleDateFormat("d MMM yyyy");
String lastDate = dateFormat.format(calendar.getTime());
String lastDate2 = dayFormat.format(calendar.getTime());
labelOutput.setText(lastDate);
labelOutput2.setText(lastDate2);
这是我想要的。//////////////////////////////////// / 它节省了我的时间。
您需要计算两个日期之间的天数
//we have two dates, now and day 0
Date d1 = new Date(0);
Date d2 = new Date();
//substract times from each other, and divide this by number milisecond in day
int days =(int)((d2.getTime()-d1.getTime()) /(1000*60*60 *24) );
//and display your result
System.out.println(days+" days = "+(days/360)+" years "+((days%360)/30)+" month "+days%30);
int totalDays = 548;
int years = (int)(totalDays / 360);
int months = (int((totalDays % 360) / 30);
int days = (int)((totalDays % 360) % 30);
System.out.println("years: " + years + "; months: " + months + "; days: " + days);
// Result: years: 1; months: 6; days: 8
还是不太明白你为什么坚持用360年,用30几个月。如果您只想从一个日期中减去天数以获得过去的正确日期,请改用:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date dateInstance = dateFormat.parse("27/07/2015");
int daysSubstracted = 548;
Calendar cal = Calendar.getInstance();
cal.setTime(dateInstance);
cal.add(Calendar.DATE, -daysSubstracted);
Date dateInPast = cal.getTime();
System.out.println(dateFormat.format(dateInPast));
// Result: 25/01/2014