طرز صحیح کار با تاریخ در زبان برنامه نویسی جاوا

ارسال پست
java2
مدیر کل سایت
پست: 178
تاریخ عضویت: پنج شنبه 13 دی 1397, 6:49 pm

طرز صحیح کار با تاریخ در زبان برنامه نویسی جاوا

پست توسط java2 » پنج شنبه 20 دی 1397, 2:24 am

کد: انتخاب همه


import java.time.LocalDateTime;
import java.time.temporal.ChronoField;

/**
 * Created by Amirsam Bahador on 10/25/2016.
 */
public class TargetClass {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        int day = now.getDayOfMonth();
        int hour = now.getHour();
        int minute = now.getMinute();
        int second = now.getSecond();
        int millis = now.get(ChronoField.MILLI_OF_SECOND);

        System.out.printf("%d-%02d-%02d %02d:%02d:%02d.%03d", year, month, day, hour, minute, second, millis);

        System.out.println();

        now = LocalDateTime.of(2012,12,12,12,12,12,12);
        year = now.getYear();
        month = now.getMonthValue();
        day = now.getDayOfMonth();
        hour = now.getHour();
        minute = now.getMinute();
        second = now.getSecond();
        millis = now.get(ChronoField.MILLI_OF_SECOND);

        System.out.printf("%d-%02d-%02d %02d:%02d:%02d.%03d", year, month, day, hour, minute, second, millis);

    }
}



خروجی

کد: انتخاب همه


2019-01-09 14:55:15.963
2012-12-12 12:12:12.000


ارسال پست