2012-12-29 182 views
0

我在写一个应用程序,其中必须显示日期。现在我想将该日期从当前日期转换为年份和月份。将日期转换为年和月

我的约会 - 29/03/2017。

我想将此日期转换为Year和Months。

对不起,我认为你无法理解我的问题。我希望今年和月份的当前日期和以上日期的差异。

对不起,我的解释。

+0

看一看在[乔达时间](http://joda-time.sourceforge.net/)库 – jlordo

回答

1

我发现我的答案使用日历类。

首先我找到了两天之间的差异,并使用那几天我找到了几年和几个月。

在这里我发布我的代码,我认为帮助别人。

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 ""; 
    } 
0

使用的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)) 
+0

我想当前的日期和上面的差异日期和年份。 –

+0

使用getTimeInMillis() –

0

长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.Then你会得到差异

+0

我想在当年和月份的当前日期和以上日期的差异。 –

3

的正确答案,你可以使用Joda时间和计算两个值LOCALDATE之间的周期(这是你在这里得到了什么)以月份和年份为单位。

例如

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"); 
+0

感谢您的回复。我是否需要一些库的时间? –

+0

只需将Joda的二进制jar包括到您的项目库文件夹 – confucius

+0

http://sourceforge.net/projects/joda-time/files/joda-time/2.1/ – confucius

0

如果你的约会对象的格式是固定的,你可以做这样的:

字符串指明MyDate = “29/03/2017”;

字符串newDate = myDate.subString(6,10)+ “ - ” + myDate.subString(3,5)

0

此方法以正常的字符串转换成日期格式

String currentDateString = "02/27/2012 17:00:00"; 
SimpleDateFormat sd = new SimpleDateFormat("mm/dd/yyyy HH:mm:ss"); 
Date currentDate = sd.parse(currentDateString); 

后你得到正式的方法

0

你应该使用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); 
相关问题