2013-09-04 17 views
19

Vorrei convertire una stringa variabile in una variabile di tipo Tempo, non in Data utilizzando Java. l'aspetto della stringa come questo 17:40Converti stringa Java in Ora, NON Data

Ho provato ad utilizzare il codice qui sotto, ma questo caso è una non variabile di tipo data ora

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); 

Tuttavia Netbean lamenta che devo inserire un'eccezione come sotto;

Qualche idea su come ottenere il tempo trascorso dalla stringa sopra.

+1

In che formato è la stringa 'originale'? ... Inoltre, Java non ha un oggetto 'Time', altri quindi' java.sql.Time' ... – MadProgrammer

+1

che ore sono? 'Java.sql.Time'? – nachokk

+0

l'ora viene da ArrayList prayerTimes String fajr_prayertime = prayerTimes.get (0); – Ossama

risposta

27
java.sql.Time timeValue = new java.sql.Time(formatter.parse(fajr_prayertime).getTime()); 
+0

Ottengo la seguente eccezione di parseexception non segnalata, deve essere catturata o dichiarata per essere generata, devo davvero creare un'eccezione per questo !! – Ossama

+0

È necessario rilevare o propagare l'eccezione. Non c'è bisogno di creare un'eccezione però. Si noti che * non * ha * di fare nulla con l'eccezione, ma in caso contrario, l'input non corretto verrà ignorato silenziosamente, causando un mal di testa in seguito. Raccomando di loggarlo come fa l'esempio del codice originale. – ash

4

Si potrebbe voler dare un'occhiata a questo esempio:

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); 

} 
11

Si potrebbe considerare Joda Time o Java 8, che ha un tipo chiamato LocalTime specificamente per un momento della giornata, senza un componente data.

codice di esempio nella Joda-Time 2.7/Java 8.

LocalTime t = LocalTime.parse("17:40") ; 
2

È possibile utilizzare il seguente codice per la modifica del valore String nel tempo equivalente:

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

Spero che questo ti aiuta.

1

provare ...

java.sql.Time.valueOf("10:30:54"); 
0

Joda Time & java.time

Sia Joda-Time e java.time (nuovo in Java 8) offrono una classe LocalTime per rappresentare un momento della giornata, senza alcuna data o fuso orario.

Codice di esempio in Joda-Time 2.3.

LocalTime localTime = new LocalTime("14:40"); 
LocalTime deadline = new LocalTime("15:30"); 
boolean meetsDeadline = localTime.isBefore(deadline); 
5
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()); 
} 
1

String Time (usando un tempo arbitrario):

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

String Time (utilizzando currentTime):

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

Tempo di stringa:

private String getCurrentTime() {  
    SimpleDateFormat dateFormat = new SimpleDateFormat("kkmmss"); 
    String currentTime = dateFormat.format(System.currentTimeMillis()); 
    return currentTime; 
} 
+0

Quale classe è 'Time'? Si prega di citare la biblioteca e il link al doc. –

+0

Quando creo l'ora di inizio; - Eclipse offre l'opzione per importare "Time" (android.text.format) – JanB

2
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:MM"); 
simpleDateFormat.format(fajr_prayertime);