简体   繁体   中英

Initialize JXDatePicker with custom date

I want to initialize a JXDatepicker with a custom date. At the moment I´m trying this:

    Date date1= new Date(2006-01-01);    
    Date date2 = new Date();
    jGeburtVon.setDate(date1);
    jGeburtBis.setDate(date2);`

Edited: this is the real code from the program ; it does compile and run and you´re right of course, new Date() initializes to today, not 01.01.1970. In this code though, date1 initializes to 01.01.1970.

I think the JXDatePicker method you're looking for is setDate(Date date)
Javadoc for the JXDatePicker can be found here .

这样,date1不是用字符串创建的,而是用很长的时间创建的:2006-01-01 = 2006-1-1 = 2004,这是1970-01-01_00:00:00之后的2004毫秒。

Using SimpleDateFormat , you can create a Date object from your String . Using that object and DateTimePicker 's setDate method you can assign the date to your object. Hope that helps.

Original text answer in spanish
utilizando el SimpleDateFormat, podes crear un objeto date a partir de tu String, luego usando ese objeto y mediante mensaje setDate del DateTimePicker,podes asignar la fecha a tu objeto.Espero te sirva.

    DateTimePicker dateChooser = new DateTimePicker();        
    Date date = new Date();
    Date fecha= new Date();
    try {
        fecha = new SimpleDateFormat("yyyy-MM-dd H:m:S").parse("2016-02-15 :00:00:00");
    } catch (ParseException e1) {
        e1.printStackTrace();
    }
    date.setTime(fecha.getTime());
    dateChooser.setFormats("dd-MM-yyyy HH:mm:ss");
    dateChooser.setTimeFormat( DateFormat.getTimeInstance( DateFormat.MEDIUM ) );
    dateChooser.setDate(date);

I got it. It has to look like this:

 SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy");
 Date date1 = df.parse("01.01.2006");

Reading the javadocs does help sometimes :) Thanks for the replies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM