Convert "Friday, February 1, 2013" to "2013-02-01" - java

How can I perform this conversion in Java?
Currently, I'm doing:
public static String formatDate(String strDateToFormat) {
try {
SimpleDateFormat sdfSource = new SimpleDateFormat("EEEE, MMMM DD, YYYY");
Date date = sdfSource.parse(strDateToFormat);
SimpleDateFormat sdfDestination = new SimpleDateFormat("yyyy-MM-dd");
return sdfDestination.format(date);
} catch (ParseException pe) {
System.err.println("Parse Exception : " + pe);
}
return null;
}
However, this results in an incorrect format. It gives me the following output:
Friday, February 1, 2013 > 2013-01-04
Thursday, January 31, 2013 > 2013-01-03

You're using DD in your parsing part, which is the day of year. You want dd instead. You also probably want yyyy (year) instead of YYYY (week year). (In most cases they're the same value, but not always.)

You're using DD in your parsing part, which is the day of year. You want dd instead.
Change also YYYY in yyyy.
You can find all patterns here.
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Your code has got two major problems and the existing answers have already solved one of them. The second and even more dangerous problem is, not using Locale with SimpleDateFormat, which is a Locale-sensitive type. Since your Date-Time string is in English, make sure to use Locale.ENGLISH or some other English-Locale. So, a correct initialization would be:
SimpleDateFormat sdfSource = new SimpleDateFormat("EEEE, MMMM d, y", Locale.ENGLISH);
Check Never use SimpleDateFormat or DateTimeFormatter without a Locale to learn more about it. Also, notice a single d which, for parsing, can cater to both single-digit as well as double-digit representation of a day-of-month. Similarly, a single y can cater to both two-digit as well as four-digit representation of a year.
Switch to the modern Date-Time API
Note that the java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API* released with Java SE 8 in March 2014.
Solution using java.time, the modern Date-Time API
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String input = "Friday, February 1, 2013";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEEE, MMMM d, u", Locale.ENGLISH);
LocalDate date = LocalDate.parse(input, dtf);
System.out.println(date);
}
}
Output:
2013-02-01
ONLINE DEMO
Some notes:
Here, you can use y instead of u but I prefer u to y.
The LocalDate#toString gives you a String in [ISO-8601 format] which is the exact same format you are expecting. So, you do not need to format LocalDate explicitly to obtain a String in this format.
Learn more about the modern Date-Time API from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Related

How to convert UTC time format to Date in Java

I have to call a web service which is expecting a Date field but they want it in the following format YYYY-MM-DDThh:mm:ss.sssZ. How can I do this?
I tried the following
OffsetDateTime transactionTime = OffsetDateTime.now(ZoneOffset.UTC);
Date.from(transactionTime.toInstant());
but this didn't work. transactionTime is 2021-06-01T15:11:09.942843400Z, but Date.from converts it to Tue Jun 01 11:11:09 EDT 2021.
BTW, I'm using Java 11
Instant.now().toString()
See that code run live at IdeOne.com.
2021-06-01T15:21:16.783779Z
That format is defined in the ISO 8601 standard. The Z on end means an offset-from-UTC of zero hours-minutes-seconds. Pronounced “Zulu”.
Use java.time.Instant to represent a moment as seen in UTC.
Going the other direction, from text to object.
Instant.parse( "2021-06-01T15:21:16.783779Z" )
If you want only milliseconds, you can lop off any microseconds and nanoseconds by truncating.
Instant.now().truncatedTo( ChronoUnit.MILLIS ) ;
Never use the legacy Date class. Use only the java.time classes.
Use DateTimeFormatter to format the Date-Time object
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
OffsetDateTime transactionTime = OffsetDateTime.now(ZoneOffset.UTC);
String formatted = transactionTime.format(dtf);
System.out.println(formatted);
}
}
Output:
2021-06-01T15:49:45.198Z
Learn more about the modern Date-Time API from Trail: Date Time.
What if I want to use java.util.Date?
For any reason, if you need to convert this object of OffsetDateTime to an object of java.util.Date, you can do so as follows:
Date date = Date.from(transactionTime.toInstant());
Note that a java.util.Date object is not a real Date-Time object like the modern Date-Time types; rather, it represents the number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT (or UTC). Since Date does not have timezone information, it applies the JVM's timezone to return the value of Date#toString in the format, EEE MMM dd HH:mm:ss z yyyy calculated from this milliseconds value. If you need to print the Date-Time in a different format and timezone, you will need to use a SimpleDateFormat with the desired format and the timezone set to the applicable one e.g.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String strDate = sdf.format(date);
System.out.println(strDate);
Output:
2021-06-01T15:49:45.198Z
Some other important notes:
The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Most of the symbols that you have used in YYYY-MM-DDThh:mm:ss.sssZ are wrong. Check the description of the symbols from the documentation pages of DateTimeFormatter and SimpleDateFormat.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Linux server showing UTC instead of EST, local showing EST

I am having trouble figuring out why the timezone for the code below keeps showing UTC instead of EST. On my local computer it show EST, even if I am in MST time but on the actual server it keeps showing UTC. Any clue?
Mon Nov 9 2015 1:58:49 PM UTC
#JsonIgnore
public String getDateCreatedFormatted() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(getDateCreated());
calendar.setTimeZone(TimeZone.getTimeZone("EST"));
SimpleDateFormat format = new SimpleDateFormat("EEE MMM d yyyy h:mm:ss a z");
return format.format(calendar.getTime());
}
You've set the calendar to EST, but you haven't set the time zone on the SimpleDateFormat, which is the one use for formatting. Just use:
format.setTimeZone(TimeZone.getTimeZone("America/New_York"));
before you format the Date. You also don't need the Calendar at all, by the looks of it:
#JsonIgnore
public String getDateCreatedFormatted() {
SimpleDateFormat format = new SimpleDateFormat("EEE MMM d yyyy h:mm:ss a z", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("America/New_York"));
return format.format(getDateCreated());
}
Also I would strongly advise you to use full time zone IDs as above, rather than the abbreviations like "EST" which are ambiguous. (There are two problems there - firstly, EST can mean different things in different locations; secondly, the US EST should always mean Eastern standard time, whereas I assume you want to format using Eastern time, either standard or daylight depending on whether daylight saving time is in effect or not.)
java.time
The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Solution using java.time, the modern Date-Time API:
#JsonIgnore
public String getDateCreatedFormatted() {
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("America/New_York"));
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM d uuuu h:mm:ss a z", Locale.ENGLISH);
return dtf.format(now);
}
ONLINE DEMO
Note: Never use SimpleDateFormat or DateTimeFormatter without a Locale.
Learn more about the modern Date-Time API from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

How to format the given time string and convert to date/time object

I am getting the time object in the form of a string from a rest service . I need to extract the time and then do some time operation.The given time string is "2015-06-16T14:58:48Z". I tried the below code , to convert the string to the time , however , getting incorrect values.
String time = "2015-06-16T14:58:48Z";
SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-DD'T'hh:mm:ss'Z'", Locale.US);
String dateInString = "2015-06-16T14:58:48Z";
Date date = formatter.parse(dateInString);
System.out.println("Original String : " + time);
System.out.println("After converting to time : " + formatter.format(date));
The output that i am getting is as below:
Original String : 2015-06-16T14:58:48Z
After converting to time : 2015-12-362T02:58:48Z
The converted date somehow is getting wrong value.Please suggest where is the mistake.Thanks.
You format string has a couple of mistakes:
Y means the week year, not the year, which is y
D means the day of the year. You should have used d, which means the day of the month.
h means a 12-hour notation time of day. Since you have 14 you should use H, which handle a 24-hour notation.
To sum it all up:
SimpleDateFormat formatter =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.US);
change SimpleDateFormat to this..
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ssX", Locale.US);
java.time
The root cause of the problem is using wrong symbols
Y (which specifies week-based-year) instead of y (which specifies year-of-era)
D (which specifies day-of-year) instead of d (which specifies day-of-month).
h (which specifies clock-hour-of-am-pm) instead of H (which specifies hour-of-day).
Check the documentation page
to learn more about these symbols.
Also, note that the legacy date-time API (java.util date-time types and their formatting API, SimpleDateFormat) is outdated and error-prone. It is recommended to stop using it completely and switch to java.time, the modern date-time API*.
Solution using the modern API:
The modern date-time API is based on ISO 8601 and does not require you to use a DateTimeFormatter object explicitly as long as the date-time string conforms to the ISO 8601 standards. Your date-time string conforms to ISO 8601 standards (or the default format used by OffsetDateTime#parse).
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "2015-06-16T14:58:48Z";
OffsetDateTime odt = OffsetDateTime.parse(strDateTime);
System.out.println(odt);
// ########################Extract time information########################
LocalTime time = odt.toLocalTime();
// You can also get it as time.getHour()
// Extract other units in a similar way
int hour = odt.getHour();
// Also using time.format(DateTimeFormatter.ofPattern("a", Locale.ENGLISH));
String amPm = odt.format(DateTimeFormatter.ofPattern("h a", Locale.ENGLISH));
System.out.println(time);
System.out.println(hour);
System.out.println(amPm);
}
}
Output:
2015-06-16T14:58:48Z
14:58:48
14
2 PM
Note:
The Z in the output is the timezone designator for zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (which has the timezone offset of +00:00 hours).
For any reason, if you need to convert this object of OffsetDateTime to an object of java.util.Date, you can do so as follows:
Date date = Date.from(odt.toInstant());
Learn more about the the modern date-time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Not able to get difference between two datetime?

Please check the below code. I am trying to get the difference but every time getting 0.
Can anybody please point me what is the problem with below code?
SimpleDateFormat sDateFormat = new SimpleDateFormat("hh:mm:ss dd/mm/yyyy");
try {
long d1 = sDateFormat.parse("10:04:00 04/04/2014").getTime();
long d2 = sDateFormat.parse("10:09:00 04/04/2014").getTime();
long difference = d2 - d1;
Log.i(TAG,">> Difference = "+difference);
} catch (ParseException e) {
e.printStackTrace();
}
your formatter does not fit the date format used.
Try:
new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
From Android Developer documentation of SimpleDateFormat, you can see M is for Month and m is for minute...
M month in year (Text) M:1 MM:01 MMM:Jan MMMM:January MMMMM:J
m minute in hour (Number) 30
So, you should change the date format from this...
hh:mm:ss dd/mm/yyyy
to this...
hh:mm:ss dd/MM/yyyy
I hope this format correction will solve your problem.
Your format, hh:mm:ss dd/mm/yyyy has two problems:
h is used for 12-Hour time format i.e. a time format with AM/PM marker which is not the case with your date-time strings. You need to use H which is used for a 24-Hour time format.
m is not used for a month. For a month, you need to use M.
Apart from this, the legacy date-time API (java.util date-time types and their formatting API, SimpleDateFormat) are outdated and error-prone. It is recommended to stop using them completely and switch to java.time, the modern date-time API*.
Demo using modern date-time API:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
public class Main {
public static void main(String args[]) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("H:m:s d/M/u");
LocalDateTime start = LocalDateTime.parse("10:04:00 04/04/2014", dtf);
LocalDateTime end = LocalDateTime.parse("10:09:00 04/04/2014", dtf);
long diff = ChronoUnit.MILLIS.between(start, end);
System.out.println(diff);
}
}
Output:
300000
Learn more about the the modern date-time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Why don't I get the year right using SimpleDateFormat in java?

I trying to parse a data in a MySql Format, I ran across SimpleDateFormat. I can get the proper day and month, but I got a strange result for the year :
date = 2009-06-22;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(date);
System.println(date);
System.println(d.getDate());
System.println(d.getMonth());
System.println(d.getYear());
Outputs :
2009-06-22
22 OK
5 Hum... Ok, months go from 0 to 11
109 o_O WTF ?
I tried changing the format to YYYY-MM-dd (got an error) and yy-MM-dd (did nothing). I am programming on Android, don't know if it's important.
For now, I bypass that using a split, but it's dirty and prevent me from using i18n features.
The year is relative to 1900. That's a "feature" of the Date class. Try to use Calender.
Thanks to Aaron, the right version :
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(date));
System.println(c.get(Calendar.YEAR));
java.time
The existing answers have already explained correctly how java.util.Date returns the year relative to 1900 and how you can get around the problem by using java.util.Calendar.
The question and existing answers use java.util date-time API and SimpleDateFormat which was the correct thing to do in 2009. In Mar 2014, the java.util date-time API and their formatting API, SimpleDateFormat were supplanted by the modern date-time API. Since then, it is highly recommended to stop using the legacy date-time API.
Using java.time, the modern date-time API:
You do not need a DateTimeFormatter: java.time API is based on ISO 8601 and therefore you do not need a DateTimeFormatter to parse a date-time string which is already in ISO 8601 format e.g. your date string, 2009-06-22 which can be parsed directly into a LocalDate instance which contains just date units.
Demo:
import java.time.LocalDate;
class Main {
public static void main(String args[]) {
String strDateTime = "2009-06-22";
LocalDate date = LocalDate.parse(strDateTime);
System.out.println(date);
System.out.printf("Day: %d, Month: %d, Year: %d", date.getDayOfMonth(), date.getMonthValue(), date.getYear());
}
}
Output:
2009-06-22
Day: 22, Month: 6, Year: 2009
ONLINE DEMO
Learn more about the modern Date-Time API from Trail: Date Time.

Categories