1

When creating a calendar object and setting the date/time using SimpleDateFormat to parse a string, is it possible to set the date and time in two separate lines of code? For example, in my SQLite db the date (mm-dd-yyyy) is stored in a separate column from the time (hh:mm). Is it kosher to do something like the following:

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdfDate = new SimpleDateFormat("MM-dd-yyyy");
SimpleDateFormat sdfTime = new SimpleDateFormat("hh:mm zzz");
cal.setTime(sdfDate.parse(DATE));
cal.setTime(sdfTime.parse(TIME));

Would the second cal.setTime line reset the date portion of the calendar object to now and just change the time?

Cœur
  • 37,241
  • 25
  • 195
  • 267
AndyS
  • 15
  • 1
  • 6

2 Answers2

1

Yes it would.

setTime() sets the the time regardless of the fact that a date contained no time value (00:00:00) or no date value (01.01.1970).

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdfDate = new SimpleDateFormat("MM-dd-yyyy hh:mm zzz");
cal.setTime(sdfDate.parse(DATE+ " " + TIME));

Should work out for you.

Jan
  • 13,738
  • 3
  • 30
  • 55
0

tl;dr

ZonedDateTime.of( 
    LocalDate.parse( "12-23-2015" , DateTimeFormatter.ofPattern( "MM-dd-yyyy") ) ,
    LocalTime.parse( "21:43" ) , 
    ZoneId.of( "Pacific/Auckland" )
)
.toString()

2015-12-23T21:43+13:00[Pacific/Auckland]

Details

The Answer by Jan is correct.

java.time

Alternatively, you could use the new date-time framework, java.time.

The java.time framework built into Java 8 and later supplants the troublesome old java.util.Date/.Calendar classes. The new classes are inspired by the highly successful Joda-Time framework, intended as its successor, similar in concept but re-architected. Defined by JSR 310. Extended by the ThreeTen-Extra project. See the Tutorial.

If your inputs lacked an offset-from-UTC, then we could treat the date and the time-of-day separately. The new classes include LocalDate to represent a date-only value without a time-of-day, and LocalTime to represent a time-only value without a date. Then you can combine them and adjust into their intended time zone.

DateTimeFormatter formatterDate = DateTimeFormatter.ofPattern( "MM-dd-yyyy");
LocalDate localDate = LocalDate.parse( "12-23-2015" , formatterDate );
LocalTime localTime = LocalTime.parse( "21:43" );
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.of( localDate , localTime , zoneId );

But your time string does contain an offset-from-UTC. So we should take the same approach as the Answer by Jan, concatenate the pair of strings and then parse.

String input = "12-23-2015" + " " + "21:43-05:00" ;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "MM-dd-yyyy HH:mmxxx");
ZonedDateTime zdt = ZonedDateTime.parse( input , formatter );

ISO 8601

By the way, in the future when serializing a date, a time, or a date-time to a string such as you did in your SQLite database I strongly recommend using the standard ISO 8601 formats: YYYY-MM-DD, HH:MM, and YYYY-MM-DDTHH:MM:SS.S±00:00. For example, 2007-12-03T10:15:30+01:00. These formats are standardized, easy for humans to read and discern, and easy for computers to parse without ambiguity.

The java.time framework parses and generates strings in these formats by default. Also, java.time extends ISO 8601 by appending the name of the time zone in square brackets. For example, 2007-12-03T10:15:30+01:00[Europe/Paris].


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.

With a JDBC driver complying with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings or 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
  • Thanks for this in depth answer. I didn't realize that there was a standard for date/time in a database. As this code has not gone to production yet I think I may change the database and relating code to match this standard. Thanks! – AndyS Dec 15 '15 at 19:26
  • @AndyS Your Question never said anything about a database. For a database, you should use the built-in date-time data types. The [SQL standard defines a few](https://en.wikipedia.org/wiki/SQL#Data_types). And then use the [java.sql.* classes](http://docs.oracle.com/javase/8/docs/api/java/sql/package-summary.html) for getting date-time data in/out of the database, until someday JDBC drivers are updated to use java.time types directly. Use the ISO 8601 formatted string representations of date-time values only as a last resort, typically exporting to text files, XML, JSON, and such -- but not db. – Basil Bourque Dec 16 '15 at 00:07
  • @AndyS P.S. Remember to up-vote any and all Answers that provide value. Eventually check the checkmark to accept an Answer if it solves your problem. – Basil Bourque Dec 16 '15 at 00:09
  • Can't up-vote just yet, don't have the reputation. As for the database, I mentioned that I was using a SQLite database, its part of an android application. Not a full out SQL database – AndyS Dec 16 '15 at 18:32