Java 获取 jDateChooser 日期到 jLabel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21012751/
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-08-13 05:55:45  来源:igfitidea点击:

Get jDateChooser date to jLabel

javaswingjlabeljcalendarjdatechooser

提问by majesticProgrammer

This is my infernal problem. nowadays i'm trying to create my project according to my Degree. I already have added jcalenderto my project in netbeans, and i already added jDateChooser to my jFrame. my problem is, when i chooseing a date from jDateChooserhow will display this date on a jLabel. i tried to using jLabel1.setText(jDateChooser1);but in this case error will occur. http://imgur.com/nMa9JMw

这是我的地狱问题。现在我正在尝试根据我的学位创建我的项目。我已经在 netbeans中将 jcalender添加到我的项目中,并且我已经将 jDateChooser 添加到我的 jFrame 中。我的问题是,当我从jDateChooser 中选择一个日期时,如何在 jLabel 上显示这个日期。我尝试使用,jLabel1.setText(jDateChooser1);但在这种情况下会发生错误。 http://imgur.com/nMa9JMw

采纳答案by MadProgrammer

First, you need to get the date from the component, something like...

首先,您需要从组件中获取日期,例如...

Date date = jDateChooser1.getDate();

Next you need to format that Datevalue to a String

接下来,您需要将该Date值格式化为String

String strDate = DateFormat.getDateInstance().format(date);

Finally, you need to set that value as the text for the lable...

最后,您需要将该值设置为标签的文本...

jLabel1.setText(strDate);

If you have particular formatting requirements, you may need to look at SimpleDateFormat

如果您有特殊的格式要求,您可能需要查看 SimpleDateFormat

回答by Indunil Girihagama

String date  = ((JTextField)jDateChooser1.getDateEditor().getUiComponent()).getText();
jLabel1.setText(date);

回答by Anuruddha Lanka Liyanarachchi

Try this,

尝试这个,

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(jDateChooser1.getSelectedDate().getTime());
jLabel1.setText(date);

回答by DASUN CHINTHAKA

import this file>>

导入这个文件>>

      import java.text.SimpleDateFormat;

Try this code >>

试试这个代码>>

    SimpleDateFormat dcn = new SimpleDateFormat("yyyy-MM-dd");
    String date = dcn.format(jDateChooser1.getDate() );
    jLabel1.setText(date.toString());

回答by Shinwar ismail

Try this:

尝试这个:

String date1 = ((JTextField) jDateChooser4.getDateEditor().getUiComponent()).getText() + "";
jLabel1.setText(date1);