【发布时间】:2012-12-29 09:37:15
【问题描述】:
我正在编写一个必须显示日期的应用程序。现在我想将该日期从当前日期转换为年和月。
我的日期是这样的 - 2017 年 3 月 29 日。
我想将此日期转换为年和月。
抱歉,我认为您无法理解我的问题。我想要年份和月份中当前日期和以上日期的差异。
对不起,我的解释。
【问题讨论】:
-
看看Joda Time库
我正在编写一个必须显示日期的应用程序。现在我想将该日期从当前日期转换为年和月。
我的日期是这样的 - 2017 年 3 月 29 日。
我想将此日期转换为年和月。
抱歉,我认为您无法理解我的问题。我想要年份和月份中当前日期和以上日期的差异。
对不起,我的解释。
【问题讨论】:
您可以使用 Joda Time 并计算两个 LocalDate 值之间的 Period(这是您在此处得到的),使用月份和年份作为单位。
例子
LocalDate dob = new LocalDate(1992, 12, 30);
LocalDate date = new LocalDate(2010, 12, 29);
Period period = new Period(dob, date, PeriodType.yearMonthDay());
System.out.println(period.getYears() + " years and " +
period.getMonths() + " months");
【讨论】:
我使用 Calender 类找到了答案。
首先我找到了两天之间的差异,然后使用这几天我找到了年份和月份。
我在这里发布我的代码,我认为这对其他人有帮助。
int days = Integer.parseInt(Utility.getDateDiffString("29/03/2017"));
int years = days/365;
int remainingDays = days - (365*years);
int months = remainingDays/30;
getDateDiffString() 方法。在这种方法中,我们需要传递结束日期
public static String getDateDiffString(String endDate)
{
try
{
Calendar cal = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date dateTwo = dateFormat.parse(endDate);
long timeOne = cal.getTimeInMillis();
long timeTwo = dateTwo.getTime();
long oneDay = 1000 * 60 * 60 * 24;
long delta = (timeTwo - timeOne) / oneDay;
if (delta > 0) {
return "" + delta + "";
}
else {
delta *= -1;
return "" + delta + "";
}
}
catch (Exception e)
{
e.printStackTrace();
}
return "";
}
【讨论】:
如果你的日期格式是固定的,你可以这样做:
String myDate = "29/03/2017";
String newDate = myDate.subString(6, 10) + "-" + myDate.subString(3, 5)
【讨论】:
此方法将普通字符串转换为日期格式
String currentDateString = "02/27/2012 17:00:00";
SimpleDateFormat sd = new SimpleDateFormat("mm/dd/yyyy HH:mm:ss");
Date currentDate = sd.parse(currentDateString);
之后你就得到了正式的方法
【讨论】:
你应该使用SimpleDateFormate !
例如:--- 您可以根据需要获取时间和日期:-
Date email_date = m.getSentDate();// this is date which you are getting
DateFormat date = new SimpleDateFormat("EEE MMM yyyy");
DateFormat time = new SimpleDateFormat("hh:mm aa");
String date_str=date.format(email_date);
String time_str=time.format(email_date);
【讨论】:
使用 Java Calendar 类从日期获取年份
Calendar c=Calendar.getInstance();
SimpleDateFormat simpleDateformat=new SimpleDateFormat("yyyy MMM");
System.out.println(simpleDateformat.format(c.getTime()));
To get difference between two date
int diffInDays = (int)( (newerDate.getTime() - olderDate.getTime())
/ (1000 * 60 * 60 * 24) )
【讨论】:
long timeDiff = (d1.getTime() - d2.getTime());
String diff=String.format("%d year(s) %d day(s) %d hour(s) %d min(s) %d sec(s)",(TimeUnit.MILLISECONDS.toDays(timeDiff)/365),TimeUnit.MILLISECONDS.toDays(timeDiff)%365,
TimeUnit.MILLISECONDS.toHours(timeDiff)
- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS
.toDays(timeDiff)),
TimeUnit.MILLISECONDS.toMinutes(timeDiff)
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
.toHours(timeDiff)),
TimeUnit.MILLISECONDS.toSeconds(timeDiff)
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
.toMinutes(timeDiff)));
System.out.println(diff);
在 d1 & d2 此处指定正确的日期。然后您将得到正确的差异答案
【讨论】:
首先将您的日期放入一个字符串变量中:
String dateToConvert = "29/03/2017";
将日历实例化为:
Calendar convertedDate = Calendar.getInstance();
将该日期设置为日历
convertedDate.set(dateToConvert);<br/>
然后使用这一行:
String datePicked = DateFormat.getDateInstance().format(convertedDate.getTime());
输出:2017 年 3 月 29 日
【讨论】: