
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Format a Message with Date in Java
To format the message with date in Java, we use the MessageFormat class and the Date class. The MessageFormat class gives us a way to produce concatenated messages which are not dependent on the language. The MessageFormat class extends the Serializable and Cloneable interfaces.
Declaration - The java.text.MessageFormat class is declared as follows −
public class MessageFormat extends Format
The MessageFormat.format(pattern, params) method formats the message and fills in the missing parts using the objects in the params array matching up the argument numbers and the array indices.
The format method has two arguments, a pattern and an array of arguments. The pattern contains placeholder in {} curly braces which have an index that indicates the position in the array where the value of the argument is stored, a time argument indicating that the filler is time and a short parameter indicating that the time is expressed in a short format. They are as follows −
String message = MessageFormat.format("{0,time,short} & UTC(0) : {1,time,short}", obj);
Let us see a program to format the message with date fillers −
Example
import java.text.MessageFormat; import java.util.Date; public class Example { public static void main(String[] args) throws Exception { Object[] obj = new Object[] { new Date(), new Date(0)}; String message = MessageFormat.format("{0,time,short} & UTC(0) : {1,time,short}", obj); System.out.println(message); } }
Output
7:35 AM & UTC(0) : 12:00 AM