If the value of the String
is 2017-09-06
, that's why you can't get it as a long
. To get a Date
from this String
, you need to use a SimpleDateFormat
:
String dateString = "2017-09-06";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(dateString);
The only problem is that a Date
is not exactly a date (with a defined day/month/year). It has no format and no timezone information, it has just the number of milliseconds since 1970-01-01T00:00Z
(January 1st 1970 at midnight in UTC) - also called "unix timestamp", "millis value" and many other names.
Using the SimpleDateFormat
above will create a Date
with the specified date (September 6th 2017). As the time isn't specified, it will default to midnight, and it will also use the JVM default timezone. So this can return different dates (different millis values) depending on the timezone set in the JVM.
If you want to use a specific timezone, just set in the formatter:
sdf.setTimeZone(TimeZone.getTimeZone("Europe/London"));
I used Europe/London
, but you can use any other timezone you want. You can get a list of all available zones (and choose the best for your case) by calling TimeZone.getAvailableIDs()
.
The API uses IANA timezones names (always in the format Region/City
, like America/Sao_Paulo
or Europe/Berlin
).
Avoid using the 3-letter abbreviations (like CST
or PST
) because they are ambiguous and not standard.
Java new Date/Time API
The old classes (Date
, Calendar
and SimpleDateFormat
) have lots of problems and design issues, and they're being replaced by the new APIs.
If you're using Java 8, consider using the new java.time API. It's easier, less bugged and less error-prone than the old APIs.
If you're using Java <= 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, you'll also need the ThreeTenABP (more on how to use it here).
The code below works for both.
The only difference is the package names (in Java 8 is java.time
and in ThreeTen Backport (or Android's ThreeTenABP) is org.threeten.bp
), but the classes and methods names are the same.
To parse this String
, you can use a LocalDate
:
// get a LocalDate from 2017-09-06
LocalDate dt = LocalDate.parse(dateString);
Then you can convert it to a Date
. In the code below I'm simulating what SimpleDateFormat
does: set the time to midnight at the JVM default timezone. In Java 8 you can use Date.from
method, and in Java 7, the ThreeTen Backport API has the org.threeten.bp.DateTimeUtils
class:
// convert to java.util.Date (Java 8)
Date d = Date.from(dt.atStartOfDay(ZoneId.systemDefault()).toInstant());
// convert to java.util.Date (Java 7 ThreeTen Backport)
Date d = DateTimeUtils.toDate(dt.atStartOfDay(ZoneId.systemDefault()).toInstant());
Just reminding that using the default timezone can be tricky, because this can be changed without notice, even at runtime, so it's better to always make it explicit which one you're using.
You can specify which timezone you want by using ZoneId.of("Europe/London")
- changing Europe/London
to the timezone you want. To get a list of all zones, use ZoneId.getAvailableZoneIds()
.
Not in the scope of the question, but you can also use an API (such as Jackson) to deal with JSON - it has built-in conversions from and to most types, including dates. In the jackson tag you'll find lots of related questions regarding date manipulation.