Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I insert special characters in SimpleDateFormat?

I want to insert the word 'at' in a SimpleDateFormat so a date would look like:

Wed, 26 May 2010 at 11:17am

I'm able to make it appear without the at, like this

Wed, 26 May 2010 11:17am

by using

SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy hh:mma"); 

How can I insert the word "at"?

like image 611
Mahmoud Saleh Avatar asked May 26 '10 10:05

Mahmoud Saleh


People also ask

What can I use instead of SimpleDateFormat?

DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe and provides additional functionality.

What is the format of SimpleDateFormat?

Class SimpleDateFormat. SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.

What is the difference between DateFormat and SimpleDateFormat?

The java SimpleDateFormat allows construction of arbitrary non-localized formats. The java DateFormat allows construction of three localized formats each for dates and times, via its factory methods.

How do you use SimpleDateFormat?

Creating SimpleDateFormat instance String pattern = "MM-dd-yyyy"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); In the example above the String pattern is the pattern which will be used to format a date and the output will be generated in that pattern as “MM-dd-yyyy”.


1 Answers

You can escape literals using single quotes.

SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy 'at' hh:mma"); 

This will output Wed, 26 May 2010 at 11:17am

like image 156
Mattias Avatar answered Sep 19 '22 19:09

Mattias