java格式化
http://tool.oschina.net/apidocs/apidoc?api=jdk-zh
https://www.jianshu.com/p/c8f16cab35e1#
参考官方的 api说明文档,format是一个抽象接口,已知的实现类有DateFormat,MessageFormat,NumberFormat
关于时间的DateFormat,我们一般是直接使用它的子类:SimpleDateFormat,具体的y,m,d,之类的用法可以参考百度
例子如下:获取当前系统的时间并格式化
package test; import java.text.SimpleDateFormat;
import java.util.Date;
import org.json.JSONArray;
import org.json.JSONObject; public class test2 {
public static void main(String[] args) { //获取系统当前时间
long currentTime = System.currentTimeMillis();
Date date = new Date(currentTime);
System.out.println(date);
//进行格式化,获取自己想要的格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分");
System.out.println(dateFormat.format(date)); }
}

关于NumberFormat,它的直接实现子类有ChoiceFormat和DecimalFormat,我们一般使用DecimalFormat
package test;
import java.text.DecimalFormat;
public class test5 {
public static void main(String[] args){
double pi=3.1415927;//圆周率
//取一位整数
System.out.println(new DecimalFormat("0").format(pi));//3
//取一位整数和两位小数
System.out.println(new DecimalFormat("0.00").format(pi));//3.14
//取两位整数和三位小数,整数不足部分以0填补。
System.out.println(new DecimalFormat("00.000").format(pi));//03.142
//取所有整数部分
System.out.println(new DecimalFormat("#").format(pi));//3
//以百分比方式计数,并取两位小数
System.out.println(new DecimalFormat("#.##%").format(pi));//314.16%
long c=299792458;//光速
//显示为科学计数法,并取五位小数
System.out.println(new DecimalFormat("#.#####E0").format(c));//2.99792E8
//显示为两位整数的科学计数法,并取四位小数
System.out.println(new DecimalFormat("00.####E0").format(c));//29.9792E7
//每三位以逗号进行分隔。
System.out.println(new DecimalFormat(",###").format(c));//299,792,458
//将格式嵌入文本
System.out.println(new DecimalFormat("光速大小为每秒,###米").format(c)); //光速大小为每秒299,792,458米
}
}
再举一个例子:比如格式化
package test; import java.text.DecimalFormat;
/*
*实现格式化,比如1格式化成[001],1001格式化成[001][001],每三位一组
*/
public class test {
public static void main(String[] args) { DecimalFormat mFormat = new DecimalFormat("[000]"); String t1 ="1";
System.out.println(mFormat.format(Integer.parseInt(t1))); String t2 = "1002";
String substring2 = t2.substring(t2.length()-3,t2.length());
String substring1 = t2.substring(0,t2.length()-2);
System.out.println(mFormat.format(Integer.parseInt(substring1))+mFormat.format(Integer.parseInt(substring2))); }
}
java格式化的更多相关文章
- JAVA格式化时间日期
JAVA格式化时间日期 import java.util.Date; import java.text.DateFormat; /** * 格式化时间类 * DateFormat.FULL = 0 * ...
- 7.20.01 java格式化输出 printf 例子
java格式化输出 printf 例子 importjava.util.Date; publicclassPrintf { publicstaticvoidmain(String[] args) { ...
- Java格式化时间
Java格式化时间 将秒或者毫秒值格式化成指定格式的时间 效果图 工具类 工具类里我只列出了一种格式的格式化方式,可以根据自己的需求,修改"yyyy-MM-dd hh:mm:ss" ...
- 【转】java格式化输出 printf 例子
[转]java格式化输出 printf 例子 转自http://www.cnblogs.com/TankMa/archive/2011/08/20/2146913.html#undefined imp ...
- java格式化时间到毫秒
转自:https://blog.csdn.net/iplayvs2008/article/details/41910835 java格式化时间到毫秒: SimpleDateFormat formatt ...
- 8.Java格式化输出
JAVA中字符串输出格式 1.使用format函数 System.out.format("%d %f",10,10.5); 2.使用Formatter类 构造函数Formatte ...
- java 格式化时间
java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); form ...
- java 格式化日期(DateFormat)
import java.text.DateFormat; import java.util.Date; /** * 格式化时间类 DateFormat.FULL = 0 * DateFormat.DE ...
- Java格式化输出
Java的格式化输出等同于String.Format,与C有很大的相似,比如 System.out.printf("%8.2f", x);在printf中,可以使用多个参数,例如: ...
- java 格式化输出方法
在javaSE5中推出了printf方法来输出文本到控制台,在java中现在有如下方法可以输出文本: 1.System.out.println(....) //输出并换行 2.System.out.f ...
随机推荐
- 如何为ASP.NET Core的强类型配置对象添加验证
原文: Adding validation to strongly typed configuration objects in ASP.NET Core 作者: Andrew Lock 译文: La ...
- 那些令人惊艳的TensorFlow扩展包和社区贡献模型
随着TensorFlow发布的,还有一个models库(仓库地址:https://github.com/tensorflow/models),里面包含官方及社群所发布的一些基于TensorFlow实现 ...
- 规避 React 组件中的 bind(this)
React 组件中处理 onClick 类似事件绑定的时候,是需要显式给处理器绑定上下文(context)的,这一度使代码变得冗余和难看. 请看如下的示例: class App extends Com ...
- Linux~连接windows的ftp,unzip出现的问题
在linux进行连接windows下的ftp服务器 ftp://192.168.2.71 输入用户名和密码登陆成功
- 初探WebAssembly
1.前言 参加完2018年上海的QCon大会,想到了会议中来自Microsoft的朱力旻大佬讲的WebAssembly,感触颇深. 我之前完全没有了解过WebAssembly,之前没有了解的原因也很简 ...
- 全文检索-Elasticsearch (一) 安装与基础概念
ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口 Elasticsearch由java开发,所以在搭建时,需先安 ...
- sql数据库快照与恢复 规则绑定
存在数据库快照的话我发进行数据库分离 CREATE DATABASE <快照名称> ON (NAME=<数据库文件名>,FILENAME='<存放地址>') AS ...
- centos系统安装第三方源EPEL
epel没安装呗 相当于扩展型软件仓库,EPEL (Extra Packages for Enterprise Linux,企业版Linux的额外软件包) 是Fedora小组维护的一个软件仓库项目,为 ...
- 图像检索(1): 再论SIFT-基于vlfeat实现
概述 基于内容的图像检索技术是采用某种算法来提取图像中的特征,并将特征存储起来,组成图像特征数据库.当需要检索图像时,采用相同的特征提取技术提取出待检索图像的特征,并根据某种相似性准则计算得到特征数据 ...
- java continue break 关键字 详解 区别 用法 标记 标签 使用 示例 联系
本文关键词: java continue break 关键字 详解 区别 用法 标记 标签 使用 示例 联系 跳出循环 带标签的continue和break 嵌套循环 深入continue ...