-1

I am trying to parse the current time and date into this simple date format.

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy-kk:mm");
Date datetoday = sdf.parse(Calendar.getInstance().getTime().toString());

However, I get this.

java.text.ParseException: Unparseable date: "Tue May 17 15:28:36 CDT 2016"

I'm still able to parse Strings like: 5/5/1991-12:00 but when given a Calendar Instance, it blows up. I really should have used JodaTime from the get go.

How can I just get the current time, into the SimpleDateFormat as it is? As I understood it, the SimpleDateFormat would take in the String and turn it into a Date once its able to parse it in. The parse would see that Tue would go into a ddd area in a SDF and May would go into a MMM if I had that in a simpleDateFormat. I dont. I have MM, and so its blowing up. I can do a M/d/yyyy in the input, so I end up giving it M, while askign for MM, and that still works.

Should I just scrap everything and go with JodaTime or is there a line or two that I'm missing?

Jayizzle
  • 532
  • 1
  • 6
  • 24
  • 4
    I don't understand. If you have a `Calendar` instance, you can get the `Date` directly from it. Why would you need to convert it to a string and parse that string? – Sotirios Delimanolis May 17 '16 at 20:54
  • 1
    JodaTime? No, you should be using JDK 8 and the java.time package. – duffymo May 17 '16 at 20:55
  • I was not clear. I have one Date follows the format of MM-dd-yyyy that I want to compare that with the current date, whos Calendar instance doesnt follow that format so they cant be compared. – Jayizzle May 18 '16 at 13:15
  • What time zone was intended for your example input string? Did you mean noon in Auckland NZ, noon in Paris FR, or noon in Montréal Québec CA? – Basil Bourque Sep 08 '16 at 18:30
  • FYI, the troublesome old 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). Joda-Time too is succeeded by java.time. – Basil Bourque Jul 11 '18 at 03:14

2 Answers2

1

This is how you obtain current date represented as a Java Date object:

Date date = new Date();

This is how you obtain current date as a formatted string:

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy-kk:mm");
String formattedDateString = sdf.format(new Date());

You can then further parse it back into a Date:

Date date = sdf.parse(formattedDateString);

But there is no point in doing that because you already had the Date when you did the .format(...) call, only in your case you used

Calendar.getInstance().getTime()

which typically gives the same result as

new Date()

If you want to parse a string into a Date then the string needs to match the format specified in SimpleDateFormat constructor. Date's toString() method that you are using returns a string in a specific format that does not match the format from your example, that's why you are getting the ParseException error.

jhncz
  • 163
  • 1
  • 8
0

String != date-time

Do not confuse a date-time object with a String that represents its value.

You should be working with objects as much as possible. Generate a String only for presentation to the user. When using strings to exchanged data with other software, use only the ISO 8601 formats.

java.time

The Question and the accepted Answer both use troublesome old legacy date-time classes, now supplanted by the java.time classes.

Your input format is not standard and is confusingly ambiguous. Is that -12:00 on the end a time zone or a time-of-day? I will guess a time-of-day. That guess means the input lacks any indication of an offset-from-UTC or a time zone. So we parse as a LocalDateTime object.

String input = "5/5/1991-12:00"
DateTimeFormatter f = DateTimeFormatter.ofPattern( "M/d/uuuu-HH:mm" );
LocalDateTime ldt = LocalDateTime.format( input , f );

ldt.toString(): 1991-05-05T12:00

A LocalDateTime object purposely lacks any offset-from-UTC or time zone. That means it does not represent a moment on the timeline, only a rough idea about possible moments. You must assign a time zone to give it meaning.

If the context of your suggestions indicates this input was meant to be in UTC, apply the constant ZoneOffset.UTC to get an OffsetDateTime.

OffsetDateTime odt = ldt.atOffset ( ZoneOffset.UTC );

odt.toString(): 1991-05-05T12:00Z

The Z on the end of a standard ISO 8601 string is short for Zulu and UTC.

On the other hand, if the context indicates a specific time zone, apply a ZoneId to get a ZonedDateTime. What time zone was intended for your example input string? Did you mean noon in Auckland NZ, noon in Paris FR, or noon in Montréal Québec CA?

ZoneId z = ZoneId.of ( "America/Montreal" );
ZonedDateTime zdt = ldt.atZone ( z );

zdt.toString(): 1991-05-05T12:00-04:00[America/Montreal]


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.

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

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

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?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

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