javafx 实现日期选择器,带有时分

来源:互联网 发布:js改变display样式 编辑:程序博客网 时间:2024/05/29 04:36
package util;


import javafx.scene.control.DatePicker;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;


import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.util.StringConverter;
/**
 * 日期选择器
 * @author 赵乔石
 *
 */
public class DateTimePicker extends DatePicker {
private ObjectProperty<LocalTime> timeValue = new SimpleObjectProperty<>();
   /* private ObjectProperty<ZonedDateTime> dateTimeValue;*/
    private ObjectProperty<LocalDateTime> dateTimeValue;
    public DateTimePicker(){
        super();
        setValue(LocalDate.now());
        setTimeValue(LocalTime.now());
        dateTimeValue=dateTimeValueProperty();


        setConverter(new StringConverter<LocalDate>() {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
            @Override
            public String toString ( LocalDate object ) {


                return dateTimeValue.get().format(formatter);
            }


            @Override
            public LocalDate fromString ( String string ) {


            Date d=LocalDateConvert.getDate(string, "yyyy-MM-dd HH:mm");
                timeValue.set(LocalDateConvert.UDateToLocalTime(d));


                LocalDate ld=LocalDate.parse(string, formatter);
                return LocalDate.parse(string, formatter);
            }
        });
    }


  /*  @Override
    protected Skin<?> createDefaultSkin () {
        return new DateTimePickerSkin(this);
    }*/


    public LocalTime getTimeValue(){
        return timeValue.get();
    }


    void setTimeValue(LocalTime timeValue){
        this.timeValue.set(timeValue);
    }


    public ObjectProperty<LocalTime> timeValueProperty(){
        return timeValue;
    }




    public LocalDateTime getDateTimeValue() {
        return dateTimeValueProperty().get();
    }




    public void setDateTimeValue (LocalDateTime dateTimeValue) {
        dateTimeValueProperty().set(dateTimeValue);
    }


    public ObjectProperty<LocalDateTime> dateTimeValueProperty(){
        if (dateTimeValue == null){
        dateTimeValue=new SimpleObjectProperty<LocalDateTime>(LocalDateTime.of(this.getValue(), timeValue.get()));
           /* dateTimeValue = new SimpleObjectProperty<>(ZonedDateTime.of(LocalDateTime.of(this.getValue(), timeValue.get()), ZoneId.systemDefault()));*/
            timeValue.addListener(t -> {
            dateTimeValue.set(LocalDateTime.of(this.getValue(),  timeValue.get()));
              /*  dateTimeValue.set(ZonedDateTime.of(LocalDateTime.of(this.getValue(), timeValue.get()), ZoneId.systemDefault()));*/
            });


            valueProperty().addListener(t -> {


            dateTimeValue.set(LocalDateTime.of(this.getValue(), timeValue.get()));
               /* dateTimeValue.set(ZonedDateTime.of(LocalDateTime.of(this.getValue(), timeValue.get()), ZoneId.systemDefault()));*/
            });
        }
        return dateTimeValue;
    }


}
0 0