问题
I am trying to format time so that the user can enter a time.
b1.setStartTime(JOptionPane.showInputDialog("Enter Start time for Booking: "));
Is there a way of doing this for the code above?
I have tried changing the startTime in booking class to a Timestamp and in the main tried to use this code:
DateFormat timeFormatter =
DateFormat.getTimeInstance(DateFormat.SHORT, b1.getStartTime());
But i struggle to wrap my head around it. All i want to do is allow the user to input a time as the short version e.g. 7:20 AM. and then i can use this time to compare against an SQL database.
To me the DateFormat.getTimeInstance() seems like the way to go. Should startTime in booking class be of type Date if i were to use this and how would i be able to format the user input?
回答1:
String datetime = [code to get time string];
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Date date = (Date) sdf.parse(datetime);
In future, you can refer to the java doc for SimpleDateFormat to learn how to set the format: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
回答2:
SimpleDateFormat might be convenient for you:
tf = new java.text.SimpleDateFormat ("h:mm a")
You can use it to parse a time:
Date d = tf.parse (textfield.getText ());
and the other way round, output a date/time in this format:
System.out.println ("Time: " + tf.fomat (date));
回答3:
First of all, you should always check the results of JOptionPane to make sure the user actually entered something and didn't click cancel.
Second, if you know how you want your user to enter the time, you need to (a) tell them how to enter it (b) use SimpleDateFormat to parse it
回答4:
The other Answers are correct but outdated.
java.time
The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
. The Joda-Time team also advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.
LocalTime
The LocalTime
class represents a time-of-day without a date and without a time zone.
Define a formatting pattern. The pattern seen here is for twelve-hour clock, expecting an hour value of 1-12.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "h:m a" );
Use that formatter to parse.
String input = "7:20 AM";
LocalTime localTime = LocalTime.parse( input , f );
Of course you should also sanitize your data inputs and add code to capture any exceptions thrown due to invalid inputs. Such code is omitted for brevity here.
Database
To query the database, you may be able to use the LocalTime
object directly via setObject
/getObject
if your JDBC driver complies with JDBC 4.2 or later.
If not, convert to the old java.sql types using new methods added to the old classes. In this situation, java.sql.Time.
java.sql.Time t = java.sql.Time.valueOf( localTime );
…and the other direction…
LocalTime localTime = t.toLocalTime();
来源:https://stackoverflow.com/questions/10338874/java-date-and-time-formatting