This question already has answers here:
Convert String Date to String date different format [duplicate]
(8 answers)
Format String yyyy-mm-dd hh:mm:ss +timezone into DateTime
(2 answers)
Converting String to Date using SimpleDateFormat is returning random date [duplicate]
(2 answers)
Closed 3 years ago.
I wrote a method to parse a date. When I'm passing the following date as an argument 2019-03-05 06:15:00, I'm getting a java.text.ParseException: Unparseable date: "2019-03-05 06:15:00".
What could be wrong with the logic?
public String convertDate(String val) throws ParseException {
return "cast('" + new SimpleDateFormat("yyyy-MM-dd HH:mm:SS").format(
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'").parse(val)) + "' as ts)";
}
You are trying to parse string 2019-03-05 06:15:00 using pattern yyyy-MM-dd'T'HH:mm'Z' that obviously does not fit.
Either change the method to following
public String convertDate(String val) throws ParseException {
return "cast('" + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'").format(
new SimpleDateFormat("yyyy-MM-dd HH:mm:SS").parse(val)) + "' as ts)";
}
or pass
2019-03-05T06:15Z
Well, nothing is wrong with your code except for this SS and change the order too.
s second-of-minute number 55
S fraction-of-second fraction 978
public static String convertDate(String val) throws ParseException {
return "cast('" + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'").format(
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(val)) + "' as ts)";
}
The Z represents the zone offset. Per documentation
Z zone-offset offset-Z +0000; -0800; -08:00;
So your String should be, for example
2019-03-05T06:15+0100
Your patterns doesn't seem to reflect what you want to accomplish. Z isn't just a simple letter.
yyyy-MM-dd'T'HH:mmZ
Anyway, avoid using SimpleDateFormat, use DateTimeFormatter instead, which is part of the new Time API for Java 8.
final String val = "2019-03-05T06:15+0100";
final TemporalAccessor parse = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mmZ").parse(val);
final String format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:SS").format(parse);
By inputting
2019-03-05T06:15+01:00
you could even just use
final TemporalAccessor parsed = DateTimeFormatter.ISO_DATE_TIME.parse(val);
Related
This question already has answers here:
Converting String to Date using SimpleDateFormat is returning random date [duplicate]
(2 answers)
how to parse output of new Date().toString()
(4 answers)
Change date format in a Java string
(22 answers)
Convert String Date to String date different format [duplicate]
(8 answers)
How to parse date string to Date? [duplicate]
(6 answers)
Closed 3 years ago.
I have a date time string like this:
Java Code:
String datetimeString = "Tue Apr 10 15:19:06 CEST 2018";
I would like to convert this to a date like this: 2018-04-10
How can I do this?
I have tried this:
Date result;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
result = formatter.parse (datetimeString);
But I get "Unparseable date"
Well since you already applied the brute force method, here's how to use the API.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateTest {
public static void main(String[] args) throws Exception {
String datetimeString = "Tue Apr 10 15:19:06 CEST 2018";
String from_format = "E MMM dd HH:mm:ss z yyyy";
String to_format = "yyyy-MM-dd";
DateTimeFormatter from_formatter = DateTimeFormatter.ofPattern(from_format);
DateTimeFormatter to_formatter = DateTimeFormatter.ofPattern(to_format);
LocalDateTime ldt = LocalDateTime.parse(datetimeString, from_formatter);
System.out.println(ldt.format(to_formatter));
}
}
In the DateTimeFormatter class it is important to understand the format symbol AND the
presentation descriptions for proper symbol count.
This question already has answers here:
Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") gives timezone as IST
(9 answers)
How to validate the DateTime string format "2018-01-22T18:23:00.000Z" in Java?
(2 answers)
parsing date/time to localtimezone
(2 answers)
Closed 3 years ago.
I am having Input Date as "2020-10-31T00:00:00Z". i want to parse this Date to get Long milliseconds.
Note: Converted milliseconds should be in Sydney Time (ie GMT+11).
FYI,
public static long RegoExpiryDateFormatter(String regoExpiryDate)
{
long epoch = 0;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(TimeZone.getTimeZone("GMT+11"));
Date date;
try {
date = df.parse(regoExpiryDate);
epoch = date.getTime();
} catch (ParseException e) {
System.out.println("Exception is:" + e.getMessage());
e.printStackTrace();
}
System.out.println("Converted regoExpiryDate Timestamp*************** " + epoch);
return epoch;
}
Output: 1604062800000 which gives Date as 30/10/2019 by using Epoch Converter, but in input i'm passing 31st as Date.
Can anyone please clarify this?
By doing df.setTimeZone(TimeZone.getTimeZone("GMT+11"));, you are asking the date formatter to interpret your string in the GMT+11 time zone. However, your string shouldn't be interpreted in that timezone. See that Z in the string? That stands for the GMT time zone, so you should have done this instead:
df.setTimeZone(TimeZone.getTimeZone("GMT"));
In fact, your string is in the ISO 8601 format for an Instant (or a "point in time", if you prefer). Therefore, you could just parse it with Instant.parse, and get the number of milliseconds with toEpochMilli:
System.out.println(Instant.parse("2020-10-31T00:00:00Z").toEpochMilli());
// prints 1604102400000
Warning: you shouldn't really use SimpleDateFormat anymore if the Java 8 APIs (i.e. Instant and such) are available. Even if they are not, you should use NodaTime or something like that.
This question already has answers here:
Illegal pattern character 'T' when parsing a date string to java.util.Date
(4 answers)
final static LocalDateTime retrieving from another class is throwing java.lang.ExceptionInInitializerError
(1 answer)
Closed 3 years ago.
I need to parse the following date format in String to Java LocalDateTime.
So I get date as String like this: 2019-09-20T12:36:39.359
I have the following unit test:
#Test
public void testDateTime() {
assertEquals(SomeObject.getLocalDate(), LocalDateTime.parse(“2019-09-20T12:36:39.359”, DateTimeFormatter.ofPattern("yyyy-MM-ddThh:mm:ss.SSS")));
}
The unit test fails with exception:
java.lang.IllegalArgumentException: Unknown pattern letter: T
at java.time.format.DateTimeFormatterBuilder.parsePattern(DateTimeFormatterBuilder.java:1661)
at java.time.format.DateTimeFormatterBuilder.appendPattern(DateTimeFormatterBuilder.java:1570)
at java.time.format.DateTimeFormatter.ofPattern(DateTimeFormatter.java:536)
How can I correctly parse the date in this format to LocalDateTime?
You can also use DateTimeFormatter.ofPattern as below
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.getDefault());
String dateStr = "2019-09-20T12:36:39.359";
LocalDateTime date = LocalDateTime.parse(dateStr, dtf);
You can use DateTimeFormatter.ISO_LOCAL_DATE_TIME as the formatter:
LocalDateTime.parse("2019-09-20T12:36:39.359", DateTimeFormatter.ISO_LOCAL_DATE_TIME);
You are comparing a String with a Date, that will say you they are not equals.
You don't even need to write the DateTimeFormatter.
Writing this code would be enough:
assertEquals("2019-09-20T12:36:39.359", LocalDateTime.parse("2019-09-20T12:36:39.359").toString());
This question already has answers here:
Java: Check the date format of current string is according to required format or not [duplicate]
(8 answers)
Closed 6 years ago.
I have a scenario where I get the date value from the xml and the date value from the website. I am able to retrieve the results, But now I need to compare the format of the retrieved results whether it is in 'MM YYYY' format or not ? How can i do that ? i have to check the format in String manner ie for e.g Apr 2010 but not 04 2010
Please can anyone of you help me ??
public class SimpleDate {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
String strDate = sdf.format(date);
sdf = new SimpleDateFormat("MMM/yyyy");
strDate = sdf.format(date);
System.out.println(strDate);
}
}
You can use a regular expresson, like this (Jan|Feb|Mar...) d{4}
This question already has answers here:
Parse String to Date with Different Format in Java
(10 answers)
Illegal pattern character 'T' when parsing a date string to java.util.Date
(4 answers)
Android Studio Convert ISO string to "America/New_York" when adding to event to calendar
(1 answer)
Change date format in a Java string
(22 answers)
Closed 9 years ago.
Hi i have the following string:2012-05-20T09:00:00.000Z
and i want to format it to be like 20/05/2012, 9am
How to do so in java?
Thanks
If you are looking for a solution to your particular case, it would be:
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse("2012-05-20T09:00:00.000Z");
String formattedDate = new SimpleDateFormat("dd/MM/yyyy, Ka").format(date);
use SimpleDateFormat to first parse() String to Date and then format() Date to String
package newpckg;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class StrangeDate {
public static void main(String[] args) {
// string containing date in one format
// String strDate = "2012-05-20T09:00:00.000Z";
String strDate = "2012-05-20T09:00:00.000Z";
try {
// create SimpleDateFormat object with source string date format
SimpleDateFormat sdfSource = new SimpleDateFormat(
"yyyy-MM-dd'T'hh:mm:ss'.000Z'");
// parse the string into Date object
Date date = sdfSource.parse(strDate);
// create SimpleDateFormat object with desired date format
SimpleDateFormat sdfDestination = new SimpleDateFormat(
"dd/MM/yyyy, ha");
// parse the date into another format
strDate = sdfDestination.format(date);
System.out
.println("Date is converted from yyyy-MM-dd'T'hh:mm:ss'.000Z' format to dd/MM/yyyy, ha");
System.out.println("Converted date is : " + strDate.toLowerCase());
} catch (ParseException pe) {
System.out.println("Parse Exception : " + pe);
}
}
}