0

I want to print time from string, i have follow this code

public class MainActivity extends AppCompatActivity {
String time = "15:30:18";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
    try {
        Date date = sdf.parse(time);
        Log.e("Time", String.valueOf(date));
        Toast.makeText(MainActivity.this, "Time : " + date , Toast.LENGTH_SHORT).show();
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

} but the ouput is Thu Jan 01 15:30:18 GMT+07:00 1970, i just want 15:30:18, anyone can help me ?

  • Quick question, if you have the string that you are parsing, why are you casting to a Date object and then back to string? – Chris Schaller Aug 24 '19 at 02:09
  • Possible duplicate of [Convert Java string to Time, NOT Date](https://stackoverflow.com/questions/18604408/convert-java-string-to-time-not-date) – Chris Schaller Aug 24 '19 at 02:10
  • FYI, the terribly troublesome date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Aug 25 '19 at 05:23

3 Answers3

0

You need to use format method

 SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
   try {
      String time = "15:30:18";
        Date date = sdf.parse(time);
        System.out.println("Time : " + sdf.format(date) );
    } catch (Exception e) {
        e.printStackTrace();
    }

See this link for more information https://stackabuse.com/how-to-get-current-date-and-time-in-java/

bakero98
  • 805
  • 7
  • 18
0

If you want a string time

DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
    try {
        Date date = sdf.parse(time);
        String time = String.valueOf(date).split(" ")[3];
        Log.e("Time", sdf.format(date));
        Toast.makeText(getApplicationContext(), "Time : " + time , Toast.LENGTH_SHORT).show();
    } catch (ParseException e) {
        e.printStackTrace();
    }

else if you want time object result you can simply do like this

 try {
        Log.e(TAG, "Time" +Time.valueOf(time)  );

    }catch (IllegalStateException ex )
    {
        Log.e(TAG, "wrong time format" );
    }
Achref ArShavin
  • 219
  • 3
  • 10
0

java.time.LocalTime

You are using the wrong class. Date represents a moment, a date with time-of-day in UTC. And that terrible class is now legacy, supplanted years ago by the modern java.time classes.

The LocalTime class represents a time-of-day without a date and without a time zone or offset-from-UTC.

String input = "15:30:18" ;
LocalTime lt = LocalTime.parse( input ) ;

ISO 8601

Create a string in standard ISO 8601 format.

String output1 = lt.toString() ;

15:30:18

Localize

Create a string in localized format.

DateTimeFormatter f = DateTimeFormatter.ofLocalizedTime( FormatStyle.MEDIUM ).withLocale( Locale.US ) ;
String output2 = lt.format( f ) ;

3:30:18 PM

Localize to Québec, just for fun.

DateTimeFormatter fQuébec = DateTimeFormatter.ofLocalizedTime( FormatStyle.MEDIUM ).withLocale( Locale.CANADA_FRENCH ) ;
String output3 = lt.format( fQuébec ) ;

15 h 30 min 18 s

See all this code run live at IdeOne.com.


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.

Where to obtain the java.time classes?

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