2

My Date constructor is deprecated and highlighted in Yellow.

How can I use Calendar.Set() to resolve this issue. I have called both import java.util.Calendar; and date.

Code is below. Thanks in advance.

Format f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
Date d = new Date(f.format(geoState.getString("fireTime")));
temp.setFireTime(d);
androidlive
  • 103
  • 2
  • 2
  • 9
  • 2
    possible duplicate of [Convert a string to a GregorianCalendar](http://stackoverflow.com/questions/2331513/convert-a-string-to-a-gregoriancalendar) – Robert Moskal Jun 25 '15 at 14:10
  • Thank you Rober for your help! – androidlive Jun 25 '15 at 14:13
  • 1
    In the future, if you have trouble with a class, the first thing you should do is read that class's documentation. The [documentation for the Date class](http://docs.oracle.com/javase/8/docs/api/java/util/Date.html) is very clear about the replacements for its deprecated methods and constructors. – VGR Jun 25 '15 at 15:11
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `ZonedDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 11 '20 at 04:29

2 Answers2

5

Use a DateFormat.parse method to convert a String to Date

String string = "January 2, 2010";
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);

In your case it will be something like this

DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
Date date = format.parse(geoState.getString("fireTime"));
temp.setFireTime(date);
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
  • 1
    Thank you Mauricio for your help. – androidlive Jun 25 '15 at 14:14
  • So I should do this way. Format f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); Date d= f.parse(geoState.getString("fireTime")); AND this what it has been suggested Date date =((DateFormat) f).parse(geoState.getString("fireTime")); – androidlive Jun 25 '15 at 14:18
  • @androidlive please provide me with your string. And avoid using single lines that are hard to read in code – Mauricio Gracia Gutierrez Jun 25 '15 at 14:25
  • Sorry, Mauricio, how I can post it so that I does not get in a line. Thanks for your help. I tried to separate lines but still goes to one line. – androidlive Jun 25 '15 at 14:33
  • @androidlive I dont mean here. I mean in your code always make it clear what is going on – Mauricio Gracia Gutierrez Jun 25 '15 at 14:34
  • Ok, got it. if (t != null && !t.equals("") && !t.equals("null")) { Format f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); Date d = new Date(f.format(geoState.getString("fireTime"))); temp.setFireTime(d); } – androidlive Jun 25 '15 at 14:35
  • The string is keep changing and here is the other part of it. t = geoState.getString("timeStateChanged"); if (t != null && !t.equals("") && !t.equals("null")) { Format f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); Date d = new Date(f.format(geoState.getString("fireTime"))); temp.setTimeStateChanged(d); } – androidlive Jun 25 '15 at 14:36
  • @androidlive I have updated my answer. is `format.parse` not `format.format` – Mauricio Gracia Gutierrez Jun 25 '15 at 14:37
  • part of my task and focus is to fix the deprecated sections of the code. The code has been written by someone else. I am a newbie and thanks for your support and help. – androidlive Jun 25 '15 at 14:39
  • @androidlive we are/were newbies at any moment, if you continue to look for better ways, pattern and follow the best practices you will refactor that and make it readable and maintainable – Mauricio Gracia Gutierrez Jun 25 '15 at 14:40
  • Correct. Thanks. I am working on that. The message gives me an error. Unhandled exception type PraseException on this line : Date date = format.parse(geoState.getString("fireTime")); – androidlive Jun 25 '15 at 14:49
  • 1
    @Please update your question and include the contents of `geoState.getString("fireTime")` – Mauricio Gracia Gutierrez Jun 25 '15 at 14:50
  • Ok, sure. I will update my code to include the output of `geoState.getString("fireTime")` – androidlive Jun 25 '15 at 15:44
  • the output is different each time and I don't have access to it right now. It is string that would be changed to date. I applied your suggestion and that gives me ParseException type error. I am trying to get rid of the deprecated Date() method. `Date d = new Date(f.format(geoState.getString("fireTime")));` – androidlive Jun 26 '15 at 14:50
  • @androidlive I need any example since the format has to match the input – Mauricio Gracia Gutierrez Jun 26 '15 at 14:54
  • I was able to fix it by adding a try catch because of the parse. Thank you for your help. – androidlive Jun 29 '15 at 12:59
  • Hi again, I have another question that I would like to get your help. I have posted it here [link](http://stackoverflow.com/questions/31116620/how-do-i-replace-the-deprecated-method-date-sethoursint?noredirect=1#comment50247785_31116620). Thanks. – androidlive Jun 29 '15 at 14:14
0

I have the same issue while migrating my project to Java 11. Here is the answer

new Date("08/10/2020");

to

DateFormat.getDateInstance().parse("08/10/2020")

Vasanth Umapathy
  • 1,571
  • 14
  • 7
  • In most locales it doesn’t work. On my Java 11 it threw an exception in 726 out of 748 available locales, and I doubt that the result was correct for all of the remaining 22 locales, which I didn’t bother to check. – Ole V.V. Aug 11 '20 at 04:20