如何在 Java 中将日期设置为 JdateChooser?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44054813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:54:17  来源:igfitidea点击:

How to Set Date to JdateChooser in java?

java

提问by harsha

enter image description here

在此处输入图片说明

I used java Jcalender_1.4.jar I have date like this,

我使用了 java Jcalender_1.4.jar 我有这样的日期,

String date = "13 Oct 2016";

字符串日期 =“2016 年 10 月 13 日”;

i want this date set to JdateChooser text box, by using this command JdateChooser.setDate();

我希望这个日期设置为 JdateChooser 文本框,通过使用这个命令 JdateChooser.setDate();

how to covert string in to date format ?

如何将字符串转换为日期格式?

回答by Waruna Wijesundara

You can do it using this easily by SimpleDateFormat command

您可以通过 SimpleDateFormat 命令轻松使用它

   String date = "13 Oct 2016";
   java.util.Date date2 = new SimpleDateFormat("yyyy-MM-dd").parse(date);
   JdateChooser.setDate(date2);

and also you can use any date format.

并且您还可以使用任何日期格式。

回答by Shinwar ismail

Calendar ca = new GregorianCalendar();
String day = ca.get(Calendar.DAY_OF_MONTH) + "";
String month = ca.get(Calendar.MONTH) + 1 + "";
String year = ca.get(Calendar.YEAR) + "";

if (day.length() == 1) {
    day = "0" + day;
}
if (month.length() == 1) {
    month = "0" + month;
}

String dd = year + "-" + month + "-" + day;

Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dd);
jDateChooser1.setDate(date);

try this set date form computer date

试试这个设定的日期表格计算机日期

回答by Ole V.V.

The following was first written as an answer to this duplicate question. I thought it would be better to have it here so we have all the answers in one place.

以下内容最初是作为对这个重复问题的回答而编写的。我认为把它放在这里会更好,这样我们就可以在一个地方找到所有的答案。

The following should work overall, only the details depend on the format of the dates in the JTable(from the other question). I have assumed that from the JTableyou get a string like 03/07/2018(I am told that this format would be commonplace in Portugal). In this case the following formatter will be fine for parsing it. If the string is in a different format, the formatter will have to be different too.

以下应该整体工作,只有细节取决于JTable(来自另一个问题)中日期的格式。我假设从JTable你得到一个字符串03/07/2018(我被告知这种格式在葡萄牙很常见)。在这种情况下,以下格式化程序可以很好地解析它。如果字符串采用不同的格式,则格式化程序也必须不同。

        DateTimeFormatter dtfFormatador = DateTimeFormatter
                .ofLocalizedDate(FormatStyle.MEDIUM)
                .withLocale(Locale.forLanguageTag("pt-PT"));

        LocalDate data = LocalDate.parse(getData, dtfFormatador);
        Instant instante = data.atStartOfDay(ZoneId.systemDefault()).toInstant();
        Date dateAntiquado = Date.from(instante);
        jdcSeletorDeDatas.setDate(dateAntiquado);

Unfortunately JDateChooser.setDate()requires an old-fashoined Dateobject, while we'd have preferred to avoid that outdated class. I am using a DateTimeFormatterfor parsing the string from the JTableinto a LocalDateand converting it to Date. LocalDateis the class from java.time, the modern Java date and time API, that we should use for a date without time of day.

不幸的是JDateChooser.setDate()需要一个过时的Date对象,而我们更愿意避免使用那个过时的类。我正在使用 aDateTimeFormatter将字符串从a解析JTable为 aLocalDate并将其转换为Date. LocalDate是来自java.time现代 Java 日期和时间 API 的类,我们应该将其用于没有时间的日期。

Edit:harsha, your string was

编辑:harsha,你的字符串是

    String date = "13 Oct 2016";

To parse a string in this format, use the following formatter:

要解析此格式的字符串,请使用以下格式化程序:

    DateTimeFormatter dateFormatter= DateTimeFormatter
            .ofLocalizedDate(FormatStyle.MEDIUM)
            .withLocale(Locale.US);

Otherwise use the code above, where jdcSeletorDeDatasis the JDateChooser.

否则,使用上面的代码,其中,jdcSeletorDeDatas是所述JDateChooser

Link:Oracle tutorial: Date Timeexplaining how to use java.time.

链接:Oracle 教程:解释如何使用java.time.

回答by Shehan Deemantha

Try to cast the value to the Datetype. This Worked for me:

尝试将值转换为Date类型。这对我有用:

int SelectRow = jTable1.getSelectedRow();
jDateChooser1.setDate((Date) jTable1.getModel().getValueAt(SelectRow, 2));