在这里,我们演示如何使用java.text.MessageFormat该类来格式化包含时间信息的消息。package org.nhooo.example.text;

import java.util.Date;

import java.util.Calendar;

import java.util.Locale;

import java.text.MessageFormat;

public class MessageFormatTime {

public static void main(String[] args) {

Date today = new Date();

Calendar calendar = Calendar.getInstance();

calendar.add(Calendar.HOUR, 7);

Date nextWeek = calendar.getTime();

// 我们希望消息为Locale.US

Locale.setDefault(Locale.US);

// 格式化包含日期信息的时间。

String message = MessageFormat.format("Now is {0} and the next " +

"7 hours is {1}", today, nextWeek);

System.out.println(message);

// 格式化时间并仅显示时间部分

message = MessageFormat.format("Now is {0, time} and the next " +

"7 hours is {1, time}", today, nextWeek);

System.out.println(message);

// 使用短格式(例如HH:mm am / pm)格式化时间

message = MessageFormat.format("Now is {0, time, short} and " +

"the next 7 hours is {1, time, short}", today, nextWeek);

System.out.println(message);

// 使用中等格式(例如HH:mm:ss am / pm)格式化时间。

message = MessageFormat.format("Now is {0, time, medium} and " +

"the next 7 hours is {1, time, medium}", today, nextWeek);

System.out.println(message);

// 使用长格式(例如HH:mm:ss am / pm TIMEZONE)格式化时间。

message = MessageFormat.format("Now is {0, time, long} and the " +

"next 7 hours is {1, time, long}", today, nextWeek);

System.out.println(message);

// 使用完整格式(例如HH:mm:ss am / pm TIMEZONE)格式化时间。

message = MessageFormat.format("Now is {0, time, full} and the " +

"next 7 hours is {1, time, full}", today, nextWeek);

System.out.println(message);

// 使用自定义模式设置时间格式。

message = MessageFormat.format("Now is {0, time, HH:mm:ss.sss} " +

"and the next 7 hours is {1, time, HH:mm:ss.sss}", today, nextWeek);

System.out.println(message);

}

}

上面的程序产生:Now is 2/15/18, 9:07 PM and the next 7 hours is 2/16/18, 4:07 AM

Now is 9:07:26 PM and the next 7 hours is 4:07:26 AM

Now is 9:07 PM and the next 7 hours is 4:07 AM

Now is 9:07:26 PM and the next 7 hours is 4:07:26 AM

Now is 9:07:26 PM CST and the next 7 hours is 4:07:26 AM CST

Now is 9:07:26 PM China Standard Time and the next 7 hours is 4:07:26 AM China Standard Time

Now is  21:07:26.026 and the next 7 hours is  04:07:26.026

java信息格式化,Java如何格式化包含时间信息的消息?相关推荐

  1. java的http请求头信息_HTTP请求头所包含的信息

    因了需要用到这些信息,所以总结一下,方便以后参阅 通过request.getHeader("User-Agent")大致可以取得用户浏览器的信息 如果里面包含: "msi ...

  2. java.sql.Date – Java SQL日期

    Java SQL Date class is part of java.sql package. java.sql.Date is a sub class of java.util.Date clas ...

  3. 论文浅尝 | TEQUILA: 一种面向时间信息的知识问答方法

    来源:CIKM'18 链接:http://delivery.acm.org/10.1145/3270000/3269247/p1807-jia.pdf?ip=223.3.116.39&id=3 ...

  4. 如何修改文件的时间信息?

    如何修改文件的时间信息?什么是文件的时间信息?其实就是文件的时间属性,包括文件的创建时间和修改时间,相信大家都应该知道,通过打开文件的属性窗口就能查看到,任何文件都有时间信息,这是文件的重要属性.有时 ...

  5. Java读取txt文件中格式化信息,显示到JTree中

    1.效果 1.1 读取格式化信息  01 代表一级节点, 0101 表示01下的二级节点,其他以此类推 1.2 显示 2.操作步骤 2.1 思考 如何能够做到能够做到,在向txt文件中追加信息时,不论 ...

  6. java date 格式化_3种 Springboot 全局时间格式化方式,别再写重复代码了

    原文:3种 Springboot 全局时间格式化方式,别再写重复代码了 掘金 作者: 程序员内点事 时间格式化在项目中使用频率是非常高的,当我们的API接口返回结果,需要对其中某一个date字段属性进 ...

  7. js 格式化 java时间格式化_javascript时间格式化

    对于从后台数据库读取出来的时间格式有时并不是我们想要的格式. 要想使用java的SimpleDateFormat是实现不了时间格式化的,不过在在http://www.javascriptsource. ...

  8. java 月份缩写_Java常用日期、时间格式化转换符

    常用日期.时间格式化转换符 转换符 说明 %te 一年中的某一天 %tb 指定语言环境的月份简称 %tB 指定语言环境的月份全称 %tA 指定语言环境的星期几全称 %ta 指定语言环境的星期几简称 % ...

  9. Java系列: JAVA字符串格式化-String.format()的使用(zz)

    常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处.format()方法有两种重 ...

最新文章

  1. lightoj 1014
  2. 前沿 | DeepMind改进超参数优化:遗传算法效果超越贝叶斯
  3. mysql handler socket_MySQL的NoSQL插件HandlerSocket
  4. 英特尔显卡linux管理_英特尔 11 代酷睿大揭秘:这次全是大招
  5. 【Python基础】Python的元组,没想象的那么简单
  6. SQL SERVER 2008 数据恢复错误的解决步骤
  7. Jenkins+Maven+SVN快速搭建持续集成环境(转)
  8. LeetCode MySQL 180. 连续出现的数字(cast)
  9. eclipse:项目启动MySQL报错:The last packet successfully received from the server was x milliseconds ago
  10. 小程序入门学习18--springboot环境配置02
  11. 快速掌握 机器学习(Machine Learning) 常用概念术语,常用算法
  12. VBA中常用的字符串处理函数
  13. 返回0-9直接的随机数
  14. 系统架构——从Memcache单点说起
  15. 《我的博客》之选择公共博客平台还是自建博客站点
  16. ROS暑期学校与ROSCon 2018
  17. ios 渐变透明背景_利用PS绘制唯美梦幻多边形背景图
  18. 爱因斯坦思考题(多维穷举)
  19. ChatGPT 客户端来喽
  20. 行测中的计算机应用题,广东公务员行测题库:行测每日一练常识应用练习题

热门文章

  1. php7 swoole 扩展,PHP7.2加入swoole扩展
  2. pandas使用iteritems函数迭代地查看dataframe中的每个数据列(iterate over each column of a dataframe)
  3. R语言在dataframe的特定索引位置添加数据行而非在dataframe末尾append数据行实战
  4. Seaborn可视化图像调整图像大小(figure size)方法实战(Adjust the Figure Size)
  5. 半监督+标签传播算法
  6. 流数据分析平台Storm简介
  7. IF、如果、Rudyard Kipling
  8. Linux下安装和使用boost库
  9. java list转成map对象_将List集合中的map对象转为List对象形式--封装类
  10. windows10下使用virtualenv虚拟技术,管理多个python版本,多个项目包环境