31

I would like to convert a variable string to a Time type variable, not Date using Java. the string look like this 17:40

I tried using the code below but this instance is a date type variable not time

String fajr_prayertime  =       prayerTimes.get(0);
DateFormat formatter = new SimpleDateFormat("HH:mm");
fajr_begins = (Date)formatter.parse(fajr_prayertime);
System.out.println(" fajr time " + fajr_begins);

However Netbean complains that I should insert an exception as below;

DateFormat formatter = new SimpleDateFormat("HH:mm");
try {
fajr_begins = (Date)formatter.parse(fajr_prayertime);
} catch (ParseException ex) {
Logger.getLogger(JavaFXApplication4.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(" fajr time " + fajr_begins);

Any idea how I can get the time out of the string above.

Ossama
  • 2,401
  • 7
  • 46
  • 83
  • 1
    What format is the original `String` in? ... Also, Java doesn't have a `Time` object, other then `java.sql.Time`... – MadProgrammer Sep 04 '13 at 02:17
  • 1
    what is time? `java.sql.Time`? – nachokk Sep 04 '13 at 02:18
  • the time come from ArrayList prayerTimes String fajr_prayertime = prayerTimes.get(0); – Ossama Sep 04 '13 at 02:24
  • I don't know what you mean Nachok, but I am getting a string similar to i.e. 15:30 and would like to convert this string to a time so that I can compare it say if time > 15:30 then... – Ossama Sep 04 '13 at 02:26

9 Answers9

32
java.sql.Time timeValue = new java.sql.Time(formatter.parse(fajr_prayertime).getTime());
ash
  • 4,867
  • 1
  • 23
  • 33
  • I get the following, unreported exception parseexception, must be caught or declared to be thrown, do I really have to create an exception for this!! – Ossama Sep 04 '13 at 02:28
  • You do have to catch, or propogate, the exception. No need to create an exception though. Note that you don't *have* to do anything with the exception, but if you don't, incorrect input will be silently ignored, leading to headaches later. I recommend logging it as the original code sample does. – ash Sep 04 '13 at 02:36
20

You might consider Joda Time or Java 8, which has a type called LocalTime specifically for a time of day without a date component.

Example code in Joda-Time 2.7/Java 8.

LocalTime t = LocalTime.parse( "17:40" ) ;
vrcca
  • 518
  • 9
  • 13
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
6

You might want to take a look at this example:

public static void main(String[] args) {

    String myTime = "10:30:54";
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
    Date date = null;
    try {
        date = sdf.parse(myTime);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    String formattedTime = sdf.format(date);

    System.out.println(formattedTime);

}
Michael 'Maik' Ardan
  • 4,213
  • 9
  • 37
  • 60
6
try {

    SimpleDateFormat format = new SimpleDateFormat("hh:mm a"); //if 24 hour format
    // or
    SimpleDateFormat format = new SimpleDateFormat("HH:mm"); // 12 hour format

    java.util.Date d1 =(java.util.Date)format.parse(your_Time);

    java.sql.Time ppstime = new java.sql.Time(d1.getTime());

} catch(Exception e) {

    Log.e("Exception is ", e.toString());
}
Paris1008
  • 69
  • 1
  • 2
5

You can use the following code for changing the String value into the time equivalent:

 String str = "08:03:10 pm";
 DateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
 Date date = (Date)formatter.parse(str);

Hope this helps you.

Sarvan
  • 706
  • 8
  • 16
Shivam
  • 674
  • 1
  • 4
  • 25
4
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:MM");
simpleDateFormat.format(fajr_prayertime);
Khusboo
  • 117
  • 1
  • 1
  • 9
2

Joda-Time & java.time

Both Joda-Time and java.time (new in Java 8) offer a LocalTime class to represent a time-of-day without any date or time zone.

Example code, identical code for both java.time and Joda-Time.

LocalTime localTime = new LocalTime( "14:40" );
LocalTime deadline = new LocalTime( "15:30" );
boolean meetsDeadline = localTime.isBefore( deadline );

enter image description here


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

try...

 java.sql.Time.valueOf("10:30:54");
Muhammad Suleman
  • 2,892
  • 2
  • 25
  • 33
  • FYI, the `java.sql.Time` class has been supplanted by the [`java.time.LocalTime`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/LocalTime.html) class, as of the adoption of JSR 310. – Basil Bourque Jun 17 '20 at 20:15
-1

String to Time (using an arbitrary time):

String myTime = "10:00:00"; 
Time startingTime = new Time (myTime);

String to Time (using currentTime):

String currentTime = getCurrentTime(); 
Time startingTime = new Time (currentTime);

Time to String:

private String getCurrentTime() {    
    SimpleDateFormat dateFormat = new SimpleDateFormat("kkmmss");
    String currentTime = dateFormat.format(System.currentTimeMillis());
    return currentTime;
}
JanB
  • 904
  • 9
  • 14