各种日期类型之间的转换

package util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;


import javafx.scene.control.DatePicker;
/**
 * localDate 日期转换器
 * @author Administrator
 *
 */
public class LocalDateConvert {
    /**
     * DateTimePicker-->String(yyyy-MM-dd HH:mm)
     * @param ld
     * @return
     */
    public static String getString(DateTimePicker ld){
        int year=ld.getValue().getYear();
        int month=ld.getValue().getMonthValue();
        int day=ld.getValue().getDayOfMonth();
        int hour=ld.getTimeValue().getHour();
        int minute=ld.getTimeValue().getMinute();
        String date=year+"-"+month+"-"+day+" "+hour+":"+minute;
        return date;
    }
    /**
     * DatePicker-->String(yyyy-MM-dd)
     * @param ld
     * @return
     */
    public static String getString(DatePicker ld){
        int year=ld.getValue().getYear();
        int month=ld.getValue().getMonthValue();
        int day=ld.getValue().getDayOfMonth();

        String date=year+"-"+month+"-"+day;
        return date;
    }
    /**
     * DatePicker-->Date
     * @param ld
     * @return
     */
    public static Date getDate(DatePicker ld){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        try {

            return sdf.parse(getString(ld));
        } catch (ParseException e) {

            e.printStackTrace();
        }
        return null;
    }
    /**
     * DateTimePicker-->date
     * @param ld
     * @return
     */
    public static Date getDate(DateTimePicker ld){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {

            return sdf.parse(getString(ld));
        } catch (ParseException e) {

            e.printStackTrace();
        }
        return null;
    }

    public static Date getDate(String date,String format){
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {

            e.printStackTrace();
        }
        return null;
    }
    /**
     * java.util.Date-->java.lang.String
     * @param date
     * @param format
     * @return
     */
    public static String getString(Date date,String format){
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        return sdf.format(date);
    }
    /**
     * java.util.Date --> java.time.LocalDateTime
     * @param date
     * @return
     */
    public static LocalDateTime UDateToLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();

        return LocalDateTime.ofInstant(instant, zone);
    }
    /**
     * java.util.Date --> java.time.LocalDate
     * @param date
     * @return
     */
    public static LocalDate UDateToLocalDate(Date date) {
        return UDateToLocalDateTime(date).toLocalDate();
    }
    /**
     * java.util.Date --> java.time.LocalTime
     * @param date
     * @return
     */
    public static LocalTime UDateToLocalTime(Date date) {
        return UDateToLocalDateTime(date).toLocalTime();
    }
    /**
     * java.time.LocalDateTime --> java.util.Date
     * @param localDateTime
     * @return
     */
    public static Date LocalDateTimeToUdate(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return Date.from(instant);
    }
    /**
     * java.time.LocalDate --> java.util.Date
     * @param localDate
     * @return
     */
    public static Date LocalDateToUdate(LocalDate localDate) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
        return  Date.from(instant);
    }
    /**
     * java.time.LocalTime --> java.util.Date
     * @param localDate
     * @param localTime
     * @return
     */
    public static Date LocalTimeToUdate(LocalDate localDate,LocalTime localTime) {

        LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return  Date.from(instant);
    }
    /**
     * java.util.Date-->java.time.LocalDate(带有分和秒)
     * @param dtp
     * @param date
     * @return
     */
    public static LocalDate DateToDateTimePickerValue(DateTimePicker dtp,Date date){
        dtp.setValue(UDateToLocalDate(date));
        dtp.setTimeValue(dtp.getTimeValue().withHour(date.getHours()));
        dtp.setTimeValue(dtp.getTimeValue().withMinute(date.getMinutes()));
        return dtp.getValue();
    }
    /**
     * java.util.Date-->java.time.LocalDate(带有分和秒)
     * @param dtp
     * @param date
     * @return
     */
    public static LocalDate DateToDateTimePickerValue(Date date){
        DateTimePicker dtp=new DateTimePicker();
        dtp.setValue(UDateToLocalDate(date));
        dtp.setTimeValue(dtp.getTimeValue().withHour(date.getHours()));
        dtp.setTimeValue(dtp.getTimeValue().withMinute(date.getMinutes()));
        return dtp.getValue();
    }

    /**
     * java.util.Date-->java.util.Date+1
     * 获取日期加1
     * @param date
     * @return
     */
    public static Date DateAddOne(Date date){
        Calendar cld = Calendar.getInstance();
        cld.setTime(date);
        cld.add(Calendar.DAY_OF_MONTH, 1);
        return cld.getTime();
    }

}

你可能感兴趣的:(javafx)