-2

That is unfair that java will close the methods of Date in next versions! yesterday I have wrote a code that parse a Data value from string and now how could I use that calendar for getting seconds, hours ? This is my code

SimpleDateFormat sdf = new SimpleDateFormat("MMM dd HH:mm:ss");
Date curDay = sdf.parse(incomingString);

and then I called curDay.GetSeconds() for example

How I need to modify my code to use a calendar ?

is this right: cal.setTime(sdf.parse(incomingString)) ???

curiousity
  • 4,703
  • 8
  • 39
  • 59
  • Uhm... Use Joda Time? After all, the time API in Java 8 is largely based on it – fge Feb 27 '14 at 13:04
  • Read the javadoc. It's explained in here. – JB Nizet Feb 27 '14 at 13:05
  • `cal.get(Calendar.SECOND)` should return the seconds. – Sebastian Höffner Feb 27 '14 at 13:06
  • Java will not "close" the methods of Date in the next versions. `java.util.Date` and `java.util.Calender` will continue to exist, they will not even be deprecated. Your code will continue to work on Java 8 and newer. But you should be glad that there's going to be a new date & time API, because `Date` and `Calendar` are crappy. – Jesper Feb 27 '14 at 13:21
  • No year in your input string?? – Basil Bourque Jan 28 '18 at 23:09

1 Answers1

1

Yeah that is right. You can change your code to use a Calendar like this.

SimpleDateFormat sdf = new SimpleDateFormat("MMM dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(incomingString)); // Note that this should be enclosed in a try-catch to handle the ParseException parse() method throws.

cal.get(Calendar.SECOND); // Get the seconds
Rahul
  • 44,383
  • 11
  • 84
  • 103
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/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/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jan 28 '18 at 23:09