我需要在我的自定义日历中设置事件,该事件将根据所选时间段重复,即每日,每周,每月或每年。我有几毫秒的开始和结束日期。
问题:
是否有任何日历或日期API,我们可以从中计算两毫秒之间的天数,周数,月数和年数。我使用了Joda库,但没有得到任何合适的方法。
我是否必须为此编写自定义代码? :-(
答案 0 :(得分:4)
您可以使用
public String getElapsedDaysText(Calendar c1, Calendar c2)
{
String elapsedDaysText = null;
try
{
long milliSeconds1 = c1.getTimeInMillis();
long milliSeconds2 = c2.getTimeInMillis();
long periodSeconds = (milliSeconds2 - milliSeconds1) / 1000;
long elapsedDays = periodSeconds / 60 / 60 / 24;
elapsedDaysText = String.format("%d days", elapsedDays);
}
catch (Exception e)
{
Logger.LogError(e);
}
return elapsedDaysText;
}
其中c1
是当前日期,c2
是将来的某个日期。如果您想计算过去的日期c2
是过去的日期,c1
是现在的日期。
您可以使用相同的方法通过进行一些更改来查找周,月和年。
答案 1 :(得分:2)
如果你的意思是说一个小女孩是:
6年,4个月和3天
......一段时间的描述?
然后您需要Joda-Time 2.3中的Period,PeriodFormatter和PeriodFormatterBuilder类。
请参阅问题Joda-Time: what's the difference between Period, Interval and Duration?
在Joda-Time doc。
中查看此discussion on Period来自Joda-Time的文档...
例如,打印年份和月份的格式化程序,如“15年零8个月”,可以构建如下:
PeriodFormatter yearsAndMonths = new PeriodFormatterBuilder()
.printZeroAlways()
.appendYears()
.appendSuffix(" year", " years")
.appendSeparator(" and ")
.printZeroRarelyLast()
.appendMonths()
.appendSuffix(" month", " months")
.toFormatter();
答案 2 :(得分:1)
如果您的日期是毫秒,那么下面提供的java课程就是您的答案,以满足您的所有需求。
http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
另外,如果您有两个日期范围,那么您可以做的只是,计算毫秒差异,将它们除以计算秒数,然后将小时数,天数和月数除以等等,以满足您的所有需求。
答案 3 :(得分:1)
我认为你可以使用它(java.util.Date)
Date date=new Date(millis);
答案 4 :(得分:1)
Calendar c1, c2;
long ms1= c1.getTimeInMillis();
long ms2= c2.getTimeInMillis();
long totalsec = (ms2- ms1) / 1000;
int days = (int) ((totalsec/ (1000*60*60*24)) % 7);
int weeks = (int) (totalsec/ (1000*60*60*24*7));
int months = weeks/30;
int years = months/365;
答案 5 :(得分:1)
CalDroid可能会解决您的问题。您可以找到它的源代码和示例here。
public static long getDayDiff(long startDay, long endDay) {
long diff = endDay - startDay;
long diffDays = diff / (24 * 60 * 60 * 1000);
return diffDays;
}
所以diffWeek = diffDays / 7; 然后是一个月,一年。
答案 6 :(得分:1)
我为此任务创建了自定义代码。在这里,您将找到在任意两个时间戳之间设置的未来事件日期(开始时间戳应小于或等于结束时间戳)。您必须修复活动持续时间,如每周,每年,每日或每月。
public class EventManipulationClass {
public EventManipulationClass(){
for (int i = 0; i < MyPetApplication.instance.eventDatasArrayList.size(); i++) {
String repType=MyPetApplication.instance.eventDatasArrayList.get(i).getRepeat();
if (repType.equals("Daily")) {
computeDaily(<long startTimeMilliSeconds>,<long endTimeMilliSeconds>);
} else if (repType.equals("Yearly")) {
computeYearly(<long startTimeMilliSeconds>,<long endTimeMilliSeconds>);
} else if (repType.equals("Monthly")) {
computeMonthly(<long startTimeMilliSeconds>,<long endTimeMilliSeconds>);
} else if (repType.equals("Weekly")) {
computeWeekely(<long startTimeMilliSeconds>,<long endTimeMilliSeconds>);
} else {
<none>
}
}
}
private String doubleDigitDate(int date){
String tempDate;
if(date<10){
tempDate="0"+date;
}else {
tempDate=""+date;
}
return tempDate;
}
private void computeWeekely(long starT,long endT){
int sMonth;
int sYear;
int sDay;
int eMonth;
int eYear;
int eDay;
int diffMonth;
int diffYear;
Calendar startCalendar=GregorianCalendar.getInstance();
startCalendar.setTimeInMillis(starT);
sMonth=startCalendar.get(Calendar.MONTH)+1;
sDay=startCalendar.get(Calendar.DAY_OF_MONTH);
sYear=startCalendar.get(Calendar.YEAR);
Calendar endCalendar=GregorianCalendar.getInstance();
endCalendar.setTimeInMillis(endT);
eMonth=endCalendar.get(Calendar.MONTH)+1;
eDay=endCalendar.get(Calendar.DAY_OF_MONTH);
eYear=endCalendar.get(Calendar.YEAR);
diffMonth=eMonth-sMonth;
diffYear=eYear-sYear;
if(diffMonth==0 && diffYear==0){
int eventDate = sDay;
int totalDaysInMonth = getNumberOfDaysInMonth(sMonth, (sYear % 400 == 0) ? true:false);
boolean isLastDay = false;
for (int x2 = 0; x2 <= 5; x2++) {
if (eventDate <= totalDaysInMonth && eventDate <= eDay) {
System.out.println("same month same year 1=====> "+ sMonth+ "-" + eventDate + "-"+ sYear);
if(eventDate==eDay){
isLastDay=true;
break;
} else {
eventDate=eventDate+7;
continue;
}
}else {
if (isLastDay) {
break;
}else{
System.out.println("same month same year 2 =====> "+ eMonth+ "-" + eDay + "-"+ eYear);
break;
}
}
}
} else if(diffMonth!=0 && diffYear==0){
int eventDate = 0;
int totalDaysInMonth = 0;
@SuppressWarnings("unused")
boolean isLastDay = false;
for (int x = sMonth; x <= eMonth; x++) {
if(x==sMonth){
eventDate=sDay;
}else{
eventDate=Math.abs(eventDate-totalDaysInMonth);
}
totalDaysInMonth=getNumberOfDaysInMonth(x, (sYear % 400 == 0) ? true:false);
for (int x2 = 0; x2 <= 5; x2++) {
if (eventDate <= totalDaysInMonth) {
if(x==eMonth && eventDate >eDay){
System.out.println("Diff month same year 1 =====> "+ x+ "-" + eDay + "-"+ sYear);
isLastDay=true;
break;
}else {
System.out.println("Diff month same year 2 =====> "+ x+ "-" + eventDate + "-"+ sYear);
if(x==eMonth && eventDate ==eDay){
isLastDay=true;
break;
} else {
eventDate=eventDate+7;
continue;
}
}
} else {
break;
}
}
}
}else {
int numberOfYear = Math.abs(sYear - eYear) ;
Integer yearsArray[] = new Integer[numberOfYear+1];
int eventDate = 0;
for (int i = 0; i <= numberOfYear; i++) {
yearsArray[i] = sYear + i;
}
int totalDaysInMonth = 0;
for (int j = 0; j < yearsArray.length ; j++) {
if (j == 0) {
for (int x = sMonth; x <= 12; x++) {
if(x==sMonth){
eventDate=sDay;
}else{
eventDate=Math.abs(eventDate-totalDaysInMonth);
}
totalDaysInMonth=getNumberOfDaysInMonth(x, (sYear % 400 == 0) ? true:false);
for (int x2 = 0; x2 <= 5; x2++) {
if (eventDate <= totalDaysInMonth) {
System.out.println("Diff month Diff year 1=====> "+ x+ "-" + eventDate + "-"+ sYear);
eventDate=eventDate+7;
} else {
break;
}
}
}
} else if (j < yearsArray.length-1) {
for (int x = 1; x <= 12; x++) {
eventDate=Math.abs(eventDate-totalDaysInMonth);
totalDaysInMonth=getNumberOfDaysInMonth(x, (sYear % 400 == 0) ? true:false);
for (int x2 = 0; x2 <= 5; x2++) {
if (eventDate <= totalDaysInMonth) {
System.out.println("Diff month Diff year 2=====> "+ x+ "-" + eventDate + "-"+ yearsArray[j]);
eventDate=eventDate+7;
} else {
break;
}
}
}
} else {
for (int x = 1; x <= eMonth; x++) {
eventDate=Math.abs(eventDate-totalDaysInMonth);
totalDaysInMonth=getNumberOfDaysInMonth(x, (sYear % 400 == 0) ? true:false);
for (int x2 = 0; x2 <= 5; x2++) {
if (eventDate <= totalDaysInMonth) {
if(x==eMonth && eventDate >eDay){
System.out.println("Diff month Diff year 3 1=====> "+ x+ "-" + eDay + "-"+ yearsArray[j]);
break;
}else {
System.out.println("Diff month Diff year 3 2=====> "+ x+ "-" + eventDate + "-"+ yearsArray[j]);
if(x==eMonth && eventDate >eDay){
System.out.println("Diff month Diff year 3 1.2=====> "+ x+ "-" + eDay + "-"+ yearsArray[j]);
break;
} else {
eventDate=eventDate+7;
continue;
}
}
} else {
if(x==eMonth && eventDate >eDay){
System.out.println("Diff month Diff year 3 1.2=====> "+ x+ "-" + eDay + "-"+ yearsArray[j]);
break;
}
break;
}
}
}
}
}
}
}
private void computeDaily(long starT,long endT){
int sMonth;
int sYear;
int sDay;
int eMonth;
int eYear;
int eDay;
int diffMonth;
int diffYear;
Calendar startCalendar=GregorianCalendar.getInstance();
startCalendar.setTimeInMillis(starT);
sMonth=startCalendar.get(Calendar.MONTH)+1;
sDay=startCalendar.get(Calendar.DAY_OF_MONTH);
sYear=startCalendar.get(Calendar.YEAR);
Calendar endCalendar=GregorianCalendar.getInstance();
endCalendar.setTimeInMillis(endT);
eMonth=endCalendar.get(Calendar.MONTH)+1;
eDay=endCalendar.get(Calendar.DAY_OF_MONTH);
eYear=endCalendar.get(Calendar.YEAR);
diffMonth=eMonth-sMonth;
diffYear=eYear-sYear;
if(diffMonth==0 && diffYear==0){
for (int i = sDay; i <= eDay; i++) {
System.out.println("same month same year 1=====> "+ sMonth+ "-" + i + "-"+ sYear);
}
}
else if (diffMonth != 0 && diffYear == 0) {
int totalDaysInMonth = 0;
for (int x = sMonth; x <= eMonth; x++) {
if(x==sMonth){
totalDaysInMonth=getNumberOfDaysInMonth(x, (sYear % 400 == 0) ? true:false);
for (int i = sDay; i <= totalDaysInMonth; i++) {
System.out.println("Diff month same year 1=====> "+ x+ "-" + i + "-"+ sYear);
}
}else if(x==eMonth){
for (int i = 1; i <= eDay; i++) {
System.out.println("Diff month same year 1=====> "+ x+ "-" + i + "-"+ sYear);
}
}
else {
totalDaysInMonth = getNumberOfDaysInMonth(x, (sYear % 400 == 0) ? true:false);
for (int i = 1; i <= totalDaysInMonth; i++) {
System.out.println("Diff month same year 1=====> "+ x+ "-" + i + "-"+ sYear);
}
}
}
}else {
int numberOfYear = Math.abs(sYear - eYear) ;
Integer yearsArray[] = new Integer[numberOfYear+1];
for (int i = 0; i <= numberOfYear; i++) {
yearsArray[i] = sYear + i;
}
int totalDaysInMonth = 0;
for (int j = 0; j < yearsArray.length ; j++) {
System.out.println(">>>>>>>>> J" +j+">>>>>>>>"+yearsArray.length);
if (j == 0) {
for (int x = sMonth; x <= 12; x++) {
if(x == sMonth){
totalDaysInMonth=getNumberOfDaysInMonth(x, (sYear % 400 == 0) ? true:false);
for (int i = sDay; i <= totalDaysInMonth; i++) {
System.out.println("Diff month Diff year 1=====> "+ x+ "-" + i + "-"+ sYear);
}
}else {
totalDaysInMonth = getNumberOfDaysInMonth(x, (sYear % 400 == 0) ? true:false);
for (int i = 1; i <= totalDaysInMonth; i++) {
System.out.println("Diff month Diff year 1=====> "+ x+ "-" + i + "-"+ sYear);
}
}
}
} else if (j < yearsArray.length-1) {
for (int x = 1; x <= 12; x++) {
totalDaysInMonth = getNumberOfDaysInMonth(x, (sYear % 400 == 0) ? true:false);
for (int i = 1; i <= totalDaysInMonth; i++) {
System.out.println("Diff month Diff year 1=====> "+ x+ "-" + i + "-"+ yearsArray[j]);
}
}
} else {
for (int x = 1; x <= eMonth; x++) {
totalDaysInMonth = getNumberOfDaysInMonth(x, (sYear % 400 == 0) ? true:false);
if(x==eMonth){
for (int i = 1; i <= eDay; i++) {
System.out.println("Diff month Diff year 1=====> "+ x+ "-" + i + "-"+ yearsArray[j]);
}
}else {
for (int i = 1; i <= totalDaysInMonth; i++) {
System.out.println("Diff month Diff year 1=====> "+ x+ "-" + i + "-"+ yearsArray[j]);
}
}
}
}
}
}
}
private void computeMonthly(long starT,long endT){
int sMonth;
int sYear;
int sDay;
int eMonth;
int eYear;
int diffMonth;
int diffYear;
Calendar startCalendar=GregorianCalendar.getInstance();
startCalendar.setTimeInMillis(starT);
sMonth=startCalendar.get(Calendar.MONTH)+1;
sDay=startCalendar.get(Calendar.DAY_OF_MONTH);
sYear=startCalendar.get(Calendar.YEAR);
Calendar endCalendar=GregorianCalendar.getInstance();
endCalendar.setTimeInMillis(endT);
eMonth=endCalendar.get(Calendar.MONTH)+1;
eYear=endCalendar.get(Calendar.YEAR);
diffMonth=eMonth-sMonth;
diffYear=eYear-sYear;
if(diffMonth==0 && diffYear==0){
System.out.println("Same month same year=====> "+sMonth + "-" + sDay + "-"+ sYear);
}else if(diffMonth!=0 && diffYear==0){
for (int x = sMonth; x <= 12; x++) {
System.out.println("Diff month same year =====> "+x + "-" + sDay + "-"+ sYear);
}
}else {
int numberOfYear = Math.abs(sYear - eYear) ;
Integer yearsArray[] = new Integer[numberOfYear+1];
for (int i = 0; i <= numberOfYear; i++) {
yearsArray[i] = sYear + i;
}
for (int j = 0; j < yearsArray.length ; j++) {
if (j == 0) {
for (int x = sMonth; x <= 12; x++) {
System.out.println("Diff month Diff year 1=====> "+ x+ "-" + sDay + "-"+ sYear);
}
} else if (j < yearsArray.length-1) {
for (int x = 1; x <= 12; x++) {
System.out.println("Diff month Diff year 2=====> "+x + "-" + sDay + "-" + yearsArray[j]);
}
} else {
for (int x = 1; x <= eMonth; x++) {
System.out.println("Diff month Diff year 3=====> "+x + "-" + sDay + "-" + yearsArray[j]);
}
}
}
}
}
private void computeYearly(long starT,long endT){
int sMonth;
int sYear;
int sDay;
int eMonth;
int eYear;
int eDay;
int diffMonth;
int diffYear;
Calendar startCalendar=GregorianCalendar.getInstance();
startCalendar.setTimeInMillis(starT);
sMonth=startCalendar.get(Calendar.MONTH)+1;
sDay=startCalendar.get(Calendar.DAY_OF_MONTH);
sYear=startCalendar.get(Calendar.YEAR);
Calendar endCalendar=GregorianCalendar.getInstance();
endCalendar.setTimeInMillis(endT);
eMonth=endCalendar.get(Calendar.MONTH)+1;
eDay=endCalendar.get(Calendar.DAY_OF_MONTH);
eYear=endCalendar.get(Calendar.YEAR);
diffMonth=eMonth-sMonth;
diffYear=eYear-sYear;
if(diffMonth==0 && diffYear==0){
System.out.println("Same month same year=====> "+sMonth + "-" + sDay + "-"+ sYear);
}else if(diffMonth!=0 && diffYear==0){
System.out.println("diff month same year=====> "+sMonth + "-" + sDay + "-"+ sYear);
}else {
int numberOfYear = Math.abs(sYear - eYear) ;
Integer yearsArray[] = new Integer[numberOfYear+1];
for (int i = 0; i <= numberOfYear; i++) {
yearsArray[i] = sYear + i;
}
for (int j = 0; j < yearsArray.length ; j++) {
if (j == 0) {
System.out.println("diff month diff year=====> "+sMonth + "-" + sDay + "-"+ sYear);
} else if (j < yearsArray.length-1) {
System.out.println("diff month diff year=====> "+sMonth + "-" + sDay + "-"+ yearsArray[j]);
} else {
System.out.println("diff month diff year=====> "+sMonth + "-" + eDay + "-"+ yearsArray[j]);
}
}
}
}
private int getNumberOfDaysInMonth(int currentMonth,boolean isLeapYear){
int days = 0;
if (currentMonth==1){
days=31;
}else if(currentMonth==2){
if(isLeapYear)
days=29;
else
days=28;
}else if(currentMonth==3){
days=31;
}else if(currentMonth==4){
days=30;
}else if(currentMonth==5){
days=31;
}else if(currentMonth==6){
days=30;
}else if(currentMonth==7){
days=31;
}else if(currentMonth==8){
days=31;
}else if(currentMonth==9){
days=30;
}else if(currentMonth==10){
days=31;
}else if(currentMonth==11){
days=30;
}else if(currentMonth==12){
days=31;
}
return days;
}
}
答案 7 :(得分:0)
这是使用Date
,Calendar
和TimeUnit
类的方法:
private String getAge(long start, long end){
long milliseconds = TimeUnit.MILLISECONDS.toMillis(Math.abs(end - start));
Calendar c = Calendar.getInstance();
c.setTimeInMillis(milliseconds);
int mYear = c.get(Calendar.YEAR)-1970;
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH)-1;
int mWeek = (c.get(Calendar.DAY_OF_MONTH)-1)/7;
return "The difference is " + mYear + " years, " + mMonth + " months, " + mDay + " days. " + mWeek + " Weeks.";
}
这是调用函数的示例:
System.out.println(getAge2(1538110800000L , 479628000000L));
输出:
The difference is 33 years, 6 months, 16 days. 2 Weeks.