0

日付を表示する必要があるアプリケーションを作成しています。今、その日付を現在の日付から年と月に変換したいと思います。

私の日付は似ています - 29/03/2017.

この日付を年と月に変換したいと思います。

申し訳ありませんが、あなたは私の質問を理解できていないと思います。現在の日付と上記の日付の年と月の差が必要です。

私の説明で申し訳ありません。

4

8 に答える 8

3

Joda Timeを使用して、月と年を単位として使用して、2つの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");
于 2012-12-29T09:40:47.990 に答える
1

Calenderクラスを使用して答えを見つけました。

最初に、2日間の違いを見つけ、その日を使用して年と月を見つけました。

ここに私のコードを投稿します。これは他の人の助けになると思います。

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 "";
    }
于 2012-12-29T13:00:05.410 に答える
0

JavaCalendarクラスを使用して日付から年を取得します

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) )
于 2012-12-29T09:38:28.753 に答える
0

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に正しい日付を指定すると、違いの正しい答えが得られます

于 2012-12-29T09:40:38.143 に答える
0

日付の形式が固定されている場合は、次のようにすることができます。

String myDate = "29/03/2017";

文字列 newDate = myDate.subString(6, 10) + "-" + myDate.subString(3, 5)

于 2012-12-29T09:43:32.750 に答える
0

通常の文字列を日付形式に変換するこのメソッド

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

その後、正式な方法を取得します

于 2012-12-29T09:45:15.197 に答える
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);
于 2012-12-29T09:46:19.083 に答える
0

まず、日付を次のように文字列変数に入れます。

String dateToConvert = "29/03/2017";

Calendar を次のようにインスタンス化します。

Calendar convertedDate = Calendar.getInstance();

その日付をカレンダーに設定する

convertedDate.set(dateToConvert);<br/>

次に、次の行を使用します。

String datePicked = DateFormat.getDateInstance().format(convertedDate.getTime());

出力: 2017 年 3 月 29 日

于 2020-06-04T15:46:40.500 に答える