4

简单的问题,但令人惊讶的是,谷歌对此几乎没有。我number of days从今年 1 月 1 日开始。如何将其转换为dateJava 中的 a?

4

7 回答 7

7

您可以简单地使用SimpleDateFormatto 转换StringDate. 该模式D可用于表示一年中的天数。

前提是你有

int numberOfDays = 42; // Arbitrary number.

然后这个计算自 1970 年 1 月 1 日(纪元)以来的天数

Date date = new SimpleDateFormat("D").parse(String.valueOf(numberOfDays));

或者,这个计算自当年 1 月 1 日以来的天数。

Date date = new SimpleDateFormat("D yyyy").parse(numberOfDays + " " + Calendar.getInstance().get(Calendar.YEAR));
于 2013-04-26T03:25:00.853 回答
3

使用日历进行日期算术

    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, 2013);
    c.set(Calendar.MONTH, Calendar.JANUARY);
    c.set(Calendar.DATE, 1);
    c.set(Calendar.HOUR, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    c.add(Calendar.DATE, numberOfDays);
    Date date = c.getTime();

请注意,由于 DST(夏令时),不同地区的结果可能会有所不同。上面的示例使用默认语言环境。

于 2013-04-26T03:16:02.713 回答
2

在日期处理方面,我发现JodaTime使用起来非常优雅。有了它,你可以这样做:

DateTime date = new DateTime().withDayOfYear(dayOfYear);
于 2013-04-26T03:32:35.703 回答
2
于 2017-02-17T23:01:29.340 回答
0

如果您是 Android 开发人员并使用 JodaTime,这就是您在 java 8 中获得等价物的方式。

LocalDate.ofYearDay("2020", 255).getDayOfWeek().getValue()

使用这个是因为ofYearDay仅在 Android >= 26 以上支持;

MutableDateTime mutableDateTime1 = new MutableDateTime();
mutableDateTime1.setYear("2020");
mutableDateTime1.setDayOfYear(255);
LocalDate.fromDateFields(mutableDateTime1.toDate()).getDayOfWeek();
于 2018-07-13T21:47:54.587 回答
0

使用GregorianCalendarsimpleDateFormat

int days = 20;
GregorianCalendar dayCountofYear = new GregorianCalendar(2020,Calendar.JANUARY, 1);
dayCountofYear.add(Calendar.DAY_OF_YEAR, days);
System.out.println("GregorianCalendar: "+ dateFormat.format(dayCountofYear.getTime()));

上述逻辑可以在函数调用中简化如下:

Date nextDate = addDaysToDate("01/01/2020", 20);
System.out.println("After Adding Number of Days:"+nextDate); // O/P:21/01/2020

完整示例:

public class DateHelper {

    public static void main(String[] args) throws ParseException {
        int days = 20;
        String initialDay = "01/01/2020";
        Date nextDate = addDaysToDate(initialDay, days);
        System.out.println("After Adding Number of Days:"+nextDate);

        /*Date firstDate = DateHelper.parseDate(initialDay, formatStr);
        int daysBetween = getDaysBetween(firstDate, nextDate);
        System.out.println(days + " - Days Between - "+daysBetween);*/
    }

    static String formatStr = "dd/MM/yyyy";
    static DateFormat dateFormat = new SimpleDateFormat(formatStr);
    static DateFormat reverseDateFormat = new SimpleDateFormat("yyyy/MM/dd");

    public static Date addDaysToDate(String specificDate, int daysCount) throws ParseException {
        Date date = dateFormat.parse(specificDate);
        GregorianCalendar gregCal = (GregorianCalendar) GregorianCalendar.getInstance(); // Current Date
        gregCal.setTime(date); // Change the Date to Provided One
        gregCal.add(Calendar.DAY_OF_YEAR, daysCount);
        Date time = gregCal.getTime();

        ZonedDateTime zdt = gregCal.toZonedDateTime();
        System.out.println("GregorianCalendar DayofWeek: "+ zdt.getDayOfWeek());
        return time;
    }
    public static String addDaysToDateStr(String specificDate, int daysCount) throws ParseException {
        String format = dateFormat.format( addDaysToDate(specificDate, daysCount) );
        System.out.println("GregorianCalendar [To a Date added Number of days leads to New Date]: "+ format);
        return format;
    }
    public static Date parseDate(String aSource, String formatStr) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat(formatStr);
        dateFormat.setLenient(false);
        return dateFormat.parse(aSource);
    }
    public static String formatDate(Date aDate, String formatStr, Calendar calendar) {
        DateFormat dateFormat = new SimpleDateFormat(formatStr);
        dateFormat.setLenient(false);
        dateFormat.setCalendar(calendar);
        return dateFormat.format(aDate);
    }
    public static String formatDate(Date aDate, String formatStr) {
        DateFormat dateFormat = new SimpleDateFormat( formatStr );
        //dateFormat.setCalendar(Calendar.getInstance(TimeZone.getTimeZone("UTF")));
        return dateFormat.format(aDate);
    }
}
于 2020-01-01T11:44:29.053 回答
-1
Calendar c = Calendar.getInstance();
         c.set(Calendar.YEAR, 2016);
         c.set(Calendar.MONTH, 8);
         c.set(Calendar.DAY_OF_YEAR, 244);
         c.set(Calendar.HOUR, 0);
         c.set(Calendar.MINUTE, 0);
         c.set(Calendar.SECOND, 0);
         c.set(Calendar.MILLISECOND, 0);
        Date date = c.getTime();
于 2017-02-17T09:32:40.693 回答