2012-12-29 184 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); 
相關問題