java中的日期和时间选择器[副本]

时间:2021-05-15 16:59:13

This question already has an answer here:

这个问题已经有了答案:

I am very new to java and I want user to input time and date on which a football match needs to be scheduled. I'm using SWING jForm as the interface. After getting that date I also want to put that in the database. After searching a lot either I couldn't search the right code or I can't understand that. I'm working on a pretty long project and left with no time to watch tutorial. Any help in this regard would be very highly appreciated.

我对java非常陌生,我希望用户输入足球比赛需要安排的时间和日期。我使用SWING jForm作为接口。在得到那个日期之后,我还想把它放到数据库中。在搜索了很多之后,我要么找不到正确的代码,要么就是无法理解。我正在做一个很长的项目,没有时间看教程。在这方面的任何帮助都将非常感谢。

Or just tell me if I have "YYYY/MM/DD HH:MM:ss:SSS" in a String then how can I convert that to Date format?

或者只是告诉我,如果我有一个字符串中的“yyyyy /MM/DD:MM:ss:SSS”,那么我如何将它转换成日期格式?

Date d = (Date)dateString didn't worked. 

Note I am using drag and drop to create items on jFrame.

注意,我使用拖放在jFrame上创建项目。

3 个解决方案

#1


1  

You may try like this:

你可以这样尝试:

SimpleDateFormat str =new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");

like this:

是这样的:

public static void main(String[] args) throws Exception {
    String str = "Sat Nov 23 20:29:30 JST 2013";
    DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH);
    Date d =  df.parse(str);  
    System.out.println(d);
}

Also check SimpleDateFormat

还要检查SimpleDateFormat

#2


1  

(a) Doing date-time work at the last minute is a bad idea. Date-time can be a surprisingly thorny topic.

(a)在最后一分钟做约会工作是个坏主意。约会时间是一个令人惊讶的棘手话题。

(b) See my answer to a similar question.

(b)见我对类似问题的回答。

My example code there uses the third-party library Joda-Time, a popular replacement for the notoriously bad java.util.Date/Calendar classes bundled with Java.

我在那里的示例代码使用第三方库Joda-Time,它是众所周知的坏java.util的流行替代品。与Java绑定的日期/日历类。

That code is just what you need, though you'll need to:

代码正是您所需要的,尽管您需要:

  • Adjust the format to your particular needs
  • 根据您的特殊需要调整格式
  • Adjust the time zones to whatever you are handling
  • 调整时区以适应你正在处理的任何事情

Do most of your date-time work within the Joda-Time framework. At the beginning and end, convert between Joda-Time DateTime objects and java.util.Date objects. Lots of Q&A here on * to show you that conversion.

在Joda-Time框架内完成大部分的日期工作。在开始和结束时,在Joda-Time DateTime对象和java.util之间进行转换。日期对象。这里有很多关于*的问答来展示这个转换。

#3


0  

Use Date.parse(String), see javadoc for more details.

使用data .parse(String),请参阅javadoc了解更多细节。

#1


1  

You may try like this:

你可以这样尝试:

SimpleDateFormat str =new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");

like this:

是这样的:

public static void main(String[] args) throws Exception {
    String str = "Sat Nov 23 20:29:30 JST 2013";
    DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH);
    Date d =  df.parse(str);  
    System.out.println(d);
}

Also check SimpleDateFormat

还要检查SimpleDateFormat

#2


1  

(a) Doing date-time work at the last minute is a bad idea. Date-time can be a surprisingly thorny topic.

(a)在最后一分钟做约会工作是个坏主意。约会时间是一个令人惊讶的棘手话题。

(b) See my answer to a similar question.

(b)见我对类似问题的回答。

My example code there uses the third-party library Joda-Time, a popular replacement for the notoriously bad java.util.Date/Calendar classes bundled with Java.

我在那里的示例代码使用第三方库Joda-Time,它是众所周知的坏java.util的流行替代品。与Java绑定的日期/日历类。

That code is just what you need, though you'll need to:

代码正是您所需要的,尽管您需要:

  • Adjust the format to your particular needs
  • 根据您的特殊需要调整格式
  • Adjust the time zones to whatever you are handling
  • 调整时区以适应你正在处理的任何事情

Do most of your date-time work within the Joda-Time framework. At the beginning and end, convert between Joda-Time DateTime objects and java.util.Date objects. Lots of Q&A here on * to show you that conversion.

在Joda-Time框架内完成大部分的日期工作。在开始和结束时,在Joda-Time DateTime对象和java.util之间进行转换。日期对象。这里有很多关于*的问答来展示这个转换。

#3


0  

Use Date.parse(String), see javadoc for more details.

使用data .parse(String),请参阅javadoc了解更多细节。