How to Replace a Placeholder in a String With a Simpledateformat Pattern

How to escape a { character in a MessageFormat pattern string?

Put a pair of single quotes around the part you don't want to be treated as parameter placeholders. i.e.

ABC '{A WELL-KNOWN MAGICIAN}' WILL PERFORM AT {0} FOR {1} HOURS.

cut next placeholder with git pretty format e.g. to cut a date string

It's possible to remove the .. using control characters. E.g.

git log --format="%h %<(12,trunc)%ci%x08%x08"

which will output something like

06bcd4c 2016-01-01
11fae28 2015-12-30

The log statement above use %x08 after the truncated format. x08 is the backspace control character that removes one output character. So %x08%x08 will remove ...

PS: Since trunc only appends .. if the string gets truncated the solution above only works if it is sure that the string will be truncated. Otherwise it removes the last two characters of the output string and not the ...

how to format date using SimpleDateFormat

If you want to read in the date "2012-02-16T00:00:00.000-0500" you should probably use a SimpleDateFormat to parse it like so:

DateFormat parseFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = parseFormat.parse("2012-02-16T00:00:00.000-0500");

Along with the rest of your code this writes:

16-Feb-2012 05:00:00

The parse format pattern letters are listed in the SimpleDateFormat documentation. The T is escaped with apostrophes.

This answer assumes Java 7, or you would be using the new date & time API from Java 8

Use different time formats according to language

I didn't think it would work, but it did. Just put the format you like into the string xml file for me it was:

<string name="date_time_format">dd MMMM yyyy, hh.mm a</string>
<string name="date_time_format">dd.MM.yyyy, HH:mm</string>

Then use it in the SDF like this:

SimpleDateFormat fmtOut = new SimpleDateFormat(context.getString(R.string.date_time_format), Locale.getDefault());

for the "Uhr" at the end of the german format i added a placeholder String that looks like this:

<string name="date_time_string">%s</string>
<string name="date_time_string">%s Uhr</string>

and i return the formated date with the String.format() method:

return String.format(context.getString(R.string.date_time_string), fmtOut.format(date));

Display current time in 12 hour format with AM/PM

Easiest way to get it by using date pattern - h:mm a, where

  • h - Hour in am/pm (1-12)
  • m - Minute in hour
  • a - Am/pm marker

Code snippet :

DateFormat dateFormat = new SimpleDateFormat("hh:mm a");

Read more on documentation - SimpleDateFormat java 7

How to create a new File with the name of the current date

You probably want to change your SimpleDateFormat to something which outputs a valid file name:

DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");

Date output formatting in Word file with docx4j

The easy answer to the side question is: upgrade to Java 9 or later.

    YearMonth from = YearMonth.of(2012, Month.MAY);
YearMonth to = YearMonth.of(2013, Month.NOVEMBER);

String format = "%1$-3tb %1$tY - %2$-3tb %2$tY";
String dateString = String.format(Locale.GERMAN, format, from, to);

System.out.println(dateString);

Mai 2012 - Nov. 2013

Java gets its locale data — including the month abbreviations used in different languages — from up to four sources. Up to and including Java 8 the default was Java’s own, but at least in Java 8 Unicode’s Common Locale Data Repository, CLDR, are distributed with Java too (not sure about Java 7). From Java 9 CLDR data are the default, but Java’s own are avaiable as COMPAT. The system property java.locale.providers controls which locale data to use. So I had expected that setting this property to CLDR or CLDR,JRE would work on Java 8, but on my Java 8 it doesn’t, Seems CLDR data are not the same in both Java versions.

In any case, while Java’s own German month abbreviations are without dots, in CLDR German month abbreviations in Java 9 are with dot. Month names up to four letters (Mai, Juni and Juli) are written in full without dot.

Java 8 answer to the side question: java.time, the modern Java date and time API allows us to define our own texts. For example:

    Map<Long, String> monthTexts = new HashMap<>(16);
monthTexts.put(1L, "Jan.");
monthTexts.put(2L, "Feb.");
monthTexts.put(3L, "Mär.");
monthTexts.put(4L, "Apr.");
monthTexts.put(5L, "Mai");
monthTexts.put(6L, "Jun.");
monthTexts.put(7L, "Jul.");
monthTexts.put(8L, "Aug.");
monthTexts.put(9L, "Sep.");
monthTexts.put(10L, "Okt");
monthTexts.put(11L, "Nov.");
monthTexts.put(12L, "Dez.");
DateTimeFormatter monthFormatter = new DateTimeFormatterBuilder()
.appendText(ChronoField.MONTH_OF_YEAR, monthTexts)
.appendPattern(" u")
.toFormatter();

String dateString = from.format(monthFormatter) + " – " + to.format(monthFormatter);

System.out.println(dateString);

Mai 2012 – Nov. 2013

Java 6 and 7 answer to the side question: get the ThreeTen Backport Library and do as in Java 8 (link at the bottom).

For the main question I don’t think I understood it. If what you want is that the ‘to’ months line up vertically, I think you need two columns in your docx document rather than one. You may specify that no vertical line be drawn between those two specific columns so the reader will see it as one.

Links
  • CLDR - Unicode Common Locale Data Repository
  • Oracle tutorial: Date Time explaining how to use java.time.
  • Java Specification Request (JSR) 310, where java.time was first described.
  • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
  • ThreeTenABP, Android edition of ThreeTen Backport
  • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.


Related Topics



Leave a reply



Submit