This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
At the moment I have a String with the folowing format :
'DD/MM/YYYY' , and I'm willing to store this in a SQLite database as a date.
Since it is impossible to store it as a datetime, I've decided Integers would be the best choice, and someone told me about the Unix epoch solution. The thing is that I'm very unfamiliar with that, and I can't seem to convert a String into a unix epoch time...
Is there a way to directly convert a String with my format into a unix epoch time, or am I doing it wrong and should I change something?
I've read this question :Unix epoch time to Java Date object But still can not find my way out with my String...
Take a look at Parsing String date to date and adjust the format to yours.
If you have a java.util.Date just use http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#getTime() to get the UNIX timestamp as long.
If in doubt, read the docs! ;)
https://developer.android.com/reference/java/text/SimpleDateFormat.html
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.parse(<value>);
Related
This question already has answers here:
How to check if string matches date pattern using time API?
(3 answers)
Parse any date in Java
(6 answers)
Format date in yyyy-MM-dd hh:mm:ss format from whatever format
(2 answers)
How to get the given date string format(pattern) in java?
(10 answers)
Closed 1 year ago.
I have several date data in the database with different format like this:
When
---
2021-11-20
2021-11-20 05:03:31
2021-11-20 04:19:25.4
19-11-2021 19:11
19/11/2021 17.39
19 November'21 16:00
19-November-2021
19/11/2021
19-Nov-2021
Now I would like to change them in one go regardless of the formatting it was to dd-mm-yyyy so they can be uniformed in Java. I have found some answers in stack overflow before such as the usage of SimpleDateTime and DateTimeFormatter, but the datetime format were seemed as only one and they wanted to change to one specific datetime form, but in my case, the datetime form seems very random. Any idea would greatly be appreciated. Thanks in advance.
P.S. I would like to sanitize the upcoming inputs from users since it has the input field for "when", but the filling format is still up to user to pick which they like. This is sure not a good practice, but changing this fundamental and most likely used would cause such unnecessary conflict.
I would suggest to create a cutom formatter using pattern and use it. Something like:
public static final MY_DATE_TIME_PARSER = new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd[['T']HH[[':']mm[[':'][ss['.'[SSS][SS][S]]]['Z']]]]").parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0).parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.parseDefaulting(ChronoField.NANO_OF_SECOND, 0).toFormatter();
And then use it as:
String string = "2021-01-01T10:20:30.Z";
LocalDateTime localDateTime = LocalDateTime.parse(string, MY_DATE_TIME_PARSER);
Try testing it with different inputs and see if it solves your purpose.
This question already has answers here:
How to get current moment in ISO 8601 format with date, hour, and minute?
(23 answers)
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 6 years ago.
I am new to Java and trying to use Calendar object to get date and time of now as a string. I am particularly stuck at object and object conversions.
Here is the format I need (as a string):
2016-03-30T14:21:00Z
If I could just get the date and time format right, I could play around with the string but I am struggling with deprecated methods.
Thank you for replies
Your best bet is to start using Java 8's new Time API (or JodaTime if you can't use Java 8)
LocalDateTime now = LocalDateTime.now();
String isoFormat = DateTimeFormatter.ISO_INSTANT.format(now.toInstant(ZoneOffset.UTC));
System.out.println(isoFormat);
outputs 2016-03-30T17:51:38.639Z (when I tested it)
Solved my question using this link:
http://beginnersbook.com/2013/05/current-date-time-in-java/
Thanks for replies, I will also look into Java 8' time API
This question already has answers here:
How to convert currentTimeMillis to a date in Java?
(13 answers)
Closed 5 years ago.
I did some searched on web about BigInt data conversion into date while I've found plenty of same question but non of them seems to work. There is 13 digit's data 1435555326831 into my database and I think this is UNIXTIME, now I want to convert it into yyyy-MM-dd HH:mm:ss form. Thank you
You can first convert the number into a long (if you receive a BigInteger, you can call BigInteger.longValue()).
Then you have two options. With the standard java.util.Date you can use:
long millis = 1435555326831L;
Date d = new Date(millis);
You can then format the date with a SimpleDateFormat for output.
If you can use Java 8's new Time API, you can create an instant and convert it to the desired time zone (your computer time zone in my example below):
Instant instant = Instant.ofEpochMilli(millis);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(fmt.format(instant.atZone(ZoneId.systemDefault())));
Note that these conversions only work if BigInteger is smaller than the maximum long size, not in general. This shouldn't be an issue, since the maximum value of a long is 2^63 - 1, but if your BigInteger is user input, you need to check for this.
Your data is on Unix timestamp and you can simply convert it by using new java.util.Date()
and here is the example of it
Java: Date from unix timestamp
This question already has an answer here:
Converting the format of the date in java
(1 answer)
Closed 1 year ago.
My requirement is to have "YYYYMMDDHHMMSS.XXX [gmt offset[:tz name]]" date format (ex: 20140425053117.694[+5.30:IST]) for a Date field in Java.
I can achieve this simply using this SimpleDateFormat class which returns a String as output. But I want the output as a "Date" object with the above Pattern.
How can I achieve this, please help!!
The java.util.Date object encapsulates a long value that represents number of milliseconds since an epoch. In simple terms, you can think of Date class as a convenient way of storing a long number representing a date along with timezone information.
Whenever you want to display the date, you can format it any way, for example with SimpleDateFormat.
Prior to JDK 1.1, the java.util.Date could be used to parse and format dates. Starting from JDK 1.1 the parsing and formatting related methods of java.util.Date are deprecated.
Read more at Date class' javadoc page
I think it's what you want:
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = isoFormat.parse("2010-05-23T09:01:02");
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to calculate time difference in java?
I have two Strings "10:00:00" and "14:00:00". I have converted them to Date Format. Now i want two compare the both time.Can anyone suggest me what to do....
use compareTo().
date1.compareTo(date2);
from java docs:
Returns:
the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value
greater than 0 if this Date is after the Date argument.
other examples
Check Date#equals, Date#after, Date#before
I preferrably use Calendar class for date related task.. It is quite to use them in some context..
You can find more helpful examples in this link
Before you do the comparison, as the timestamps are still in the String format, you need to convert them to java.util.Date and the simplest way is to assign an arbitrary date part to both time strings (e.g. concatenate both time strings to "1/1/1970 "), then convert them into Date using SimpleDateFormat, then get time in milliseconds and do the comparison.