I am using intl pakage's DateFormat class to format dates with a format like this:
_standardDateFormat = DateFormat('MMM dd, yyyy');
This produces strings like Feb 01, 2023.
How can I have the date without the prefix of 0 when it's a single-digit date? I would like to show Feb 1, 2023. I have tried d, dd, c but they all render with the prefixed 0.
If it's a double-digit date, I would of course like to show both digits: Mar 13, 2023.
Update: the single d format is indeed working, but I needed to restart flutter build to see the effect (not sure why).
Related
This question already has answers here:
Parsing time-of-day with am/pm using Java DateTimeFormatter class [duplicate]
(3 answers)
Can't parse String to LocalDate (Java 8)
(2 answers)
Closed last month.
The community reviewed whether to reopen this question last month and left it closed:
Original close reason(s) were not resolved
I am trying to format the String date, time and zone information.
LocalDateTime.parse("Dec 01, 2019 1:00:00 PM +00:00", DateTimeFormatter.ofPattern("MMM dd, YYYY hh:mm:ss a XXX"));
ZonedDateTime.parse("Dec 01, 2019 1:00:00 PM +00:00", DateTimeFormatter.ofPattern("MMM dd, YYYY hh:mm:ss a XXX"));
Please note for project support reasons i cannot use above java 8.
I am unable to get this parse work, I did try a lot of versions before i posted here. any support is appreciated.
Exception:
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Dec 01, 2019 1:00:00 PM +00:00' could not be parsed at index 13
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:494)
at com.parse.Test.main(Test.java:10)
Your date-time string, "Dec 01, 2019 1:00:00 PM +00:00" has timezone offset of 00:00 hours. The java.time API provides us with OffsetDateTime to parse this type of date-time string to.
Demo:
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
class Main {
public static void main(String[] args) {
DateTimeFormatter parser = DateTimeFormatter.ofPattern("MMM dd, uuuu h:mm:ss a XXX", Locale.ENGLISH);
OffsetDateTime odt = OffsetDateTime.parse("Dec 01, 2019 1:00:00 PM +00:00", parser);
System.out.println(odt);
}
}
Output:
2019-12-01T13:00Z
ONLINE DEMO
Learn more about the modern Date-Time API from Trail: Date Time.
What went wrong with your code?
You have used hh whereas your string has only one digit in the hour.
You have used Y instead of y. Note that Y is used for week-based-year. The right symbol for the intended purpose is y or u. Here, you can use y instead of u but I prefer u to y.
I also recommend you understand the importance of using a Locale while using date-time parsing/formatting API. Check Never use SimpleDateFormat or DateTimeFormatter without a Locale to learn more about it.
This Worked
LocalDateTime.parse("Dec 01, 2019 1:00:00 PM +00:00", DateTimeFormatter.ofPattern("MMM dd, yyyy h:mm:ss a XXX"));
ZonedDateTime.parse("Dec 01, 2019 1:00:00 PM +00:00", DateTimeFormatter.ofPattern("MMM dd, yyyy h:mm:ss a XXX"));
What I changed was:
We use lower case yyyy for the year. Format pattern strings are case sensitive.
We use just one h for the hour of day since it may be just one digit (1 in my example). A single h does accept two digits too, though, so times with 10, 11 or 12 will not pose any problem.
I have a LocalDateTime object and I would like to format this, to have printouts like:
Tue 23. Nov. Therefore, I used a DateTimeFormatter like:
val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("e dd. LLL")
But unfortunately I get Tue 23. 11 The month is a number and no letters!?
The correct format pattern string is E dd. MMM. Excuse my Java syntax.
private static final DateTimeFormatter DATE_FORMATTER
= DateTimeFormatter.ofPattern("E dd. MMM", Locale.ENGLISH);
Also remember to specify desired locale for your formatter.
Trying it out:
LocalDate date = LocalDate.of(2021, Month.NOVEMBER, 23);
String formatted = date.format(DATE_FORMATTER);
System.out.println(formatted);
Output is the desired:
Tue 23. Nov
Spelling out how my format pattern is different:
I am using upper case E for the abbreviation of the day of week. Lower case e should give you the number of the day of week like 2 for Tuesday. eee should work for the abbreviation too.
I am using MMM for the abbreviation of the month. LLL is for the standalone form. Some languages use a different form of the month depending on whether the day of month is present or not. A language may for example use the nominative for the month alone and the genetive with a day number, a bit the differene between November and of November. Since you have the day included, you should not use pattern letter L here. Funnily for some languages that have not got a stand-alone form (like English), Java gives you the number instead when you specify LLL.
Edit: you asked:
How would that look for "November" fully written out? "MMM" works for
"Dec."
The documentation that you linked to in another comment gives the answer:
Text: The text style is determined based on the number of pattern letters used. Less than 4 pattern letters will use the short form.
Exactly 4 pattern letters will use the full form. …
So use MMMM instead of MMM:
private static final DateTimeFormatter DATE_FORMATTER
= DateTimeFormatter.ofPattern("E dd. MMMM", Locale.ENGLISH);
Tue 23. November
Documentation link: DateTimeFormatter
According to the documentation, it seems that it is only possible to manipulate dates with a numeric format, and not letters.
Does a package exist if I want to display date with the format Thur, Aug 23 ?
If not, how could I do this ?
Thank you !
You can create your own DateFormat using it's default constructor.
The format you want is EEEE, MMM d.
For Example print(new DateFormat("EEEE, MMM d").format(new DateTime.now())); should print Wedn, Jun 26 for today.
I am getting {item.pubDate} from XML and the value is:
Mon, 02 Mar 2015 14:35:47 +0000
so I did this:
<#assign starting_point = item.pubDate?index_of(",")>
<#assign date="${item.pubDate?substring(starting_point + 1)}" />
${date?datetime("dd MMM yyyy hh:mm:ss z")?date}<br>
and the result is: Mar 2, 2015.
My question is, can we change value from Mar to March and if we can then what is the best way to do it? I could have if/elseif statements in freemarker and assign each three letter months to full month name but it looks not good. Any advice/tips will be greatly appreciated. thanks.
It doesn't mater, MMM will parse both Mar and March. The only important thing is to have at least 3 M-s, as http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html says:
If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.
Yes, FreeMarker follows the same datetime formatting rules as Java. Use the ?string built in for dates. You can do:
${date?datetime("dd MMM yyyy hh:mm:ss z")?string("MMMM dd, yyyy")}
Source: http://freemarker.org/docs/ref_builtins_date.html#ref_builtin_string_for_date
date.parse() method of groovy detects date DD and year yyyy correctly but is unable to detect the month as mmm.. As in
println new Date().parse("DD-MMM-yyyy", '22-MAR-2011')
yields output as
Sat Jan 22 00:00:00 GMT+05:30 2011
Why is the month march as MAR picked up as Jan? What can I do to make it detect the month in mmm format?
The problem is actualy that you are using DD - that means day in year
Correct way:
println new Date().parse("dd-MMM-yyyy", '22-MAR-2011')
Quick tip when formatting dates try using the reverse and see what comes out:
println new Date().format("dd-MMM-yyyy")
Groovy uses SimpleDateFormat under the hood but that's not that important since most date libraries use the same format conventions.