Changing date format in Android getting ParseException [duplicate] - java

This question already has answers here:
Java - Unparseable date
(3 answers)
how to parse output of new Date().toString()
(4 answers)
Closed 2 years ago.
I'm new in Android and Java development.
I have this day type "Fri Jan 27 00:00:00 GMT+00:00 1995". <-- (input)
I want to get "1995/27/01" <-- (output)
I'm using this code :
String inputPattern = "EEE MMM dd HH:mm:ss z yyyy";
String outputPattern = "yyyy-MM-dd";
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
Date date = inputFormat.parse("Fri Jan 27 00:00:00 GMT+00:00 1995");
String str = outputFormat.format(date);
but I get a ParseException.
Any idea why?

If you were using java.time (available from Java 8), you could do it like this:
public static void main(String[] args) {
// example input String
String datetime = "Fri Jan 27 00:00:00 GMT+00:00 1995";
// define a pattern that parses the format of the input String
String inputPattern = "EEE MMM dd HH:mm:ss O uuuu";
// define a formatter that uses this pattern for parsing the input
DateTimeFormatter parser = DateTimeFormatter.ofPattern(inputPattern, Locale.ENGLISH);
// use the formatter in order to parse a zone-aware object
ZonedDateTime zdt = ZonedDateTime.parse(datetime, parser);
// then output it in the built-in ISO format once,
System.out.println("date, time and zone (ISO):\t"
+ zdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
// then extract the date part
LocalDate dateOnly = zdt.toLocalDate();
// and output that date part using a formatter with a date-only pattern
System.out.println("date only as desired:\t\t"
+ dateOnly.format(DateTimeFormatter.ofPattern("uuuu/dd/MM")));
}
The output of this piece of code is
date, time and zone (ISO): 1995-01-27T00:00:00Z
date only as desired: 1995/27/01

Maybe add applyPattern function in code
String inputPattern = "EEE MMM dd HH:mm:ss z yyyy";
String outputPattern = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(inputPattern);
Date date = sdf.parse("Fri Jan 27 00:00:00 GMT+00:00 1995");
sdf.applyPattern(outputPattern);
String str = sdf.format(date);

Related

How can i convert String to Date when it has "TRT" in it

String sDate = "06.08.2020" // 06 day 08 month 2020 is year
This is the date i have in my txt file. I use them in JTable. To sort the table i convert them to date with this DateFormatter.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
And it does convert the string to date as this.
LocalDate date = LocalDate.parse(sDate,formatter);
//The date : Thu Aug 06 00:00:00 TRT 2020
Now i need to convert it like the first date 06.08.2020.
But i can't use date as input. Because i get it from JTable so i get it as String.
So i tryed this code.
String sDate1 = "Thu Aug 06 00:00:00 TRT 2020";// The date i get from JTable
LocalDate lastdate = LocalDate.parse(sDate1,formatter);
sDate1 = formatter.format(lastdate);
But i get an error as this Text 'Thu Aug 06 00:00:00 TRT 2020' could not be parsed at index 0.
So this cone not works fine : LocalDate lastdate = LocalDate.parse(sDate1,formatter);
I cant see where is the problem.
I cannot reproduce the behaviour you describe. The following code worked fine for me:
import java.util.*;
import java.text.*;
public class MyClass {
public static void main(String args[]) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String date = "06.08.2020";
Date date1 = sdf.parse(date);
String result = sdf.format(date1);
System.out.println("Date = " + result);
}
}
Output: Date = 06.08.2020
That being said, if at all possible you should switch to the new java.time.* API.
Where your code failed:
SimpleDateFormat sdf1=new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
String dateStr = "06.08.2020";
sdf1.parse(dateStr);
As you can see, the pattern of the SimpleDateFormat and that of the date string do not match and therefore, this code will throw ParseException.
How to make it work?
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String dateStr = "06.08.2020";
Date date = sdf.parse(dateStr);
You must have already got why it worked. It worked because the pattern of the SimpleDateFormat matches with that of the dateStr string.
Can I format the Date object (i.e. date) into the original string?
Yes, just use the same format which you used to parse the original string as shown below:
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String dateStr = "06.08.2020";
Date date = sdf.parse(dateStr);
// Display in the default format
System.out.println(date);
// Format into the string
dateStr = sdf.format(date);
System.out.println(dateStr);
A piece of advice:
I recommend you switch from the outdated and error-prone java.util date-time API and SimpleDateFormat to the modern java.time date-time API and the corresponding formatting API (package, java.time.format). Learn more about the modern date-time API from Trail: Date Time.
Using the modern date-time API:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
String dateStr = "06.08.2020";
LocalDate date = LocalDate.parse(dateStr, formatter);
// Display in the default format
System.out.println(date);
// Format into the string
dateStr = formatter.format(date);
System.out.println(dateStr);
I don't see any difference using the legacy API and the modern API:
That's true for this simple example but when you will need to do complex operations using date and time, you will find the modern date-time API smart and clean while the legacy API complex and error-prone.
Demo:
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// Given date-time string
String strDate = "Thu Aug 06 00:00:00 TRT 2020";
// Replace TRT with standard time-zone string
strDate = strDate.replace("TRT", "Europe/Istanbul");
// Define formatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzzz yyyy");
// Parse the date-time string into ZonedDateTime
ZonedDateTime zdt = ZonedDateTime.parse(strDate, formatter);
System.out.println(zdt);
// If you wish, convert ZonedDateTime into LocalDateTime
LocalDateTime ldt = zdt.toLocalDateTime();
System.out.println(ldt);
}
}
Output:
2020-08-06T00:00+03:00[Europe/Istanbul]
2020-08-06T00:00

How do I Date format for dd MMM yyyy, as in 01 Apr 2020 [duplicate]

This question already has answers here:
java DateTimeFormatterBuilder fails on testtime [duplicate]
(2 answers)
Closed 2 years ago.
I have a pdf file where I want to know if the next line is a date, or just a string (there are two types of formats of the listing, and knowing if I've arrived at a date is important.) The trouble is, there appears to be no way to use date formatting to arrive at a date of 01 Apr 2020
LocalDate date = parseDate( "dd MMM yyyy", "01 Apr 2020" );
Throws ... Text '01 Apr 2020' could not be parsed at index 3
private static LocalDate parseDate( final String format, final String s ) {
final DateTimeFormatter df = DateTimeFormatter.ofPattern( format );
LocalDate ld; // Check if this was a legal LocalDate.
try {
ld = LocalDate.parse(s, df);
} catch (java.time.format.DateTimeParseException pe) {
System.out.println( pe.getMessage() );
ld = null; // This will signal an error
}
return ld;
}
Is there really no way to parse that format of date, like a bank uses in their pdf?
Replace
final DateTimeFormatter df = DateTimeFormatter.ofPattern( format );
With
final DateTimeFormatter df = DateTimeFormatter.ofPattern( format , Locale.US );
Hopefully, this will resolve your issue.
I believe you're using java8, You can do
LocalDate date = LocalDate.parse("01 Apr 2020", DateTimeFormatter.ofPattern("dd MMM yyyy", Locale.ROOT));
Edited: After pointed out that it doesn't work for all locales. Locale.ROOT should be used for neutral locale.

Date to local time kotlin

I have a date object with
"Sun Apr 26 11:44:00 GMT+03:00 2020"
I tried to format it but it will always ignore the timezone "+3:00" and show the hour without changing it
what I have tried :
val sdf = SimpleDateFormat("dd MM yyyy HH:mm:ss", Locale.US)
return sdf.format(this)
I don't exactly understand your question but I guess you meant you need DateTime related to a particular zone. In kotlin we've ZonedDateTime class
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
val londonZone = ZoneId.of("Europe/London")
val londonCurrentDateTime = ZonedDateTime.now(londonZone)
println(londonCurrentDateTime) // Output: 2018-01-25T07:41:02.296Z[Europe/London]
val londonDateAndTime = londonCurrentDateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.MEDIUM))
println(londonDateAndTime) // Output: Thursday, January 25, 2018 7:40:34 AM
You also might need to take a look at LocalDateTime class.
If you just want to parse a datetime String with the given pattern and then convert the result to a different offset, you can go like this:
fun main() {
// provide the source String
val datetime = "Sun Apr 26 11:44:00 GMT+03:00 2020"
// provide a pattern for parsing
val parsePattern = "E MMM dd HH:mm:ss O yyyy";
// parse the String to an OffsetDateTime
val parsedOffsetDateTime = java.time.OffsetDateTime.parse(
datetime,
java.time.format.DateTimeFormatter.ofPattern(parsePattern))
// print the result using the default format
println(parsedOffsetDateTime)
// then get the same moment in time at a different offset
val adjustedOffsetDateTime = parsedOffsetDateTime.withOffsetSameInstant(
java.time.ZoneOffset.of("+02:00"))
// and print that, too, in order to see the difference
println(adjustedOffsetDateTime)
}
which produces the output
2020-04-26T11:44+03:00
2020-04-26T10:44+02:00
The format string depends on which SimpleDateFormat you use:
If using java.text.SimpleDateFormat with API Level 24+, the format string needs to be:
"EEE MMM d HH:mm:ss 'GMT'XXX yyyy"
If using android.icu.text.SimpleDateFormat, the format string needs to be:
"EEE MMM d HH:mm:ss OOOO yyyy".
Demo (in plain Java)
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Jerusalem"));
Date date = new Date(1587890640000L); // date object with "Sun Apr 26 11:44:00 GMT+03:00 2020"
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss 'GMT'XXX yyyy", Locale.US);
String dateString = sdf.format(date);
System.out.println("Formatted : " + dateString);
Date parsed = sdf.parse(dateString);
System.out.println("Parsed back: " + parsed);
System.out.println("Millis since Epoch: " + parsed.getTime());
Output
Formatted : Sun Apr 26 11:44:00 GMT+03:00 2020
Parsed back: Sun Apr 26 11:44:00 IDT 2020
Millis since Epoch: 1587890640000

How to get in this date format? [duplicate]

This question already has answers here:
How can I change the date format in Java? [duplicate]
(10 answers)
Change date format in a Java string
(22 answers)
Java string to date conversion
(17 answers)
Closed 4 years ago.
I am getting two date fields from JSON as text like this May 22 12:05:41 UTC 2018 and 2018-05-22 12:05:41.512 but I have to change to MM-dd-yyyy HH:mm:ss format.
Just to be clear - date/time objects are format agnostic. They are simply containers for the amount of time which has passed since a fixed point in time (usually the Unix Epoch), so you can't change their format per se.
However, you can, generate a String of a prescribed pattern.
When dealing with date/time in Java you should make use of the date/time APIs introduced in Java 8 (or the ThreeTen back port)
For example...
String date1 = "May 22 12:05:41 UTC 2018";
DateTimeFormatter format1 = DateTimeFormatter.ofPattern("MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
LocalDateTime ldt1 = LocalDateTime.parse(date1, format1);
String date2 = "2018-05-22 12:05:41.512";
DateTimeFormatter format2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);
LocalDateTime ldt2 = LocalDateTime.parse(date2, format2);
DateTimeFormatter format3 = DateTimeFormatter.ofPattern("MM-dd-yyyy HH:mm:ss", Locale.ENGLISH);
System.out.println(ldt1.format(format3));
System.out.println(ldt2.format(format3));
Which outputs...
05-22-2018 12:05:41
05-22-2018 12:05:41
Since one of your inputs has a timezone associated with it, it would be appropriate to take it into consideration
ZonedDateTime zdt = ZonedDateTime.parse(date1, format1);
LocalDateTime ldt1 = zdt.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();
which outputs (for my current location)
05-22-2018 22:05:41
You can use this code
DateFormat format = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
DateFormat inputFormat1 = new SimpleDateFormat("MMM dd HH:mm:ss z yyyy");
DateFormat inputFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String date1 = "May 22 12:05:41 UTC 2018";
String date2 = "2018-05-22 12:05:41.512";
System.out.println(format.format(inputFormat1.parse(date1)));
System.out.println(format.format(inputFormat2.parse(date2)));

Date formatting using JODA - Malformed at AM

I am trying to format the following date
Thu, 15 Jan 2015, 9:56 AM
Using the following:
public String parse(String oldDate){
final String OLD_FORMAT = "EEE, d MMM yyyy, HH:mm:ss zz";
final String NEW_FORMAT = "yyyy/MM/dd";
// August 12, 2010
String oldDateString = oldDate;
String newDateString;
DateTimeFormatter formatterOld = DateTimeFormat.forPattern(OLD_FORMAT);
DateTimeFormatter formatterNew = DateTimeFormat.forPattern(NEW_FORMAT);
LocalDate localDate = formatterOld.parseLocalDate( oldDateString );
return newDateString = formatterNew.print( localDate );
}
I am getting a
Caused by: java.lang.IllegalArgumentException: Invalid format: "Thu, 15 Jan 2015, 9:56 AM" is malformed at " AM"
How do I represent AM/PM properly in that date format? I tried using Z but get the same and changed to zz but invain. What is the correct representation for AM/PM?
I also checked with "hh:mm a" but that again does nto seem to help.
Z and z are for TimeZones - you want a which is the format-code for half-day.
The formatting codes are describe in the JavaDoc for org.joda.time.format.DateTimeFormat
The code below works for me (running on a Java 8 JRE with Joda 2.6)
public static void main(String[] args) {
String format = "EEE, d MMM yyyy, HH:mm a";
DateTimeFormatter formatter = DateTimeFormat.forPattern(format);
final LocalDate date = formatter.parseLocalDate("Thu, 15 Jan 2015, 9:56 AM");
System.out.println(date);
}

Categories