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 ...
随机推荐
- JVM基础系列第4讲:从源代码到机器码,发生了什么?
在上篇文章我们聊到,无论什么语言写的代码,其到最后都是通过机器码运行的,无一例外.那么对于 Java 语言来说,其从源代码到机器码,这中间到底发生了什么呢?这就是今天我们要聊的. 如下图所示,编译器可 ...
- 使用ML.NET和Azure Function进行机器学习 - 第2部分
本文是<使用ML.NET和AzureFunction进行机器学习 - 第1部分>的续篇. 像机器一样识别鸢尾花 回顾第1部分,您将使用Visual Studio创建一个新的Azure Fu ...
- Spring Cloud中服务的发现与消费
之前没注意,微信公众号的图片不能引用到其他地方,本文图片显示不正常,原图在写完博客后已经删了,,,,,,所以本文小伙伴可以移步这里https://mp.weixin.qq.com/s/GoIZdwt5 ...
- 深入理解Spring AOP思想
什么是AOP?AOP解决了什么问题? 在传统的开发模式中,以下层次的是非常常见的一种,业务层每一个方法都要有重复的事务代码 如何改善这个问题? AOP希望将A.B 这些分散在各个业务逻辑中的相同代码, ...
- 对于ASP.NET MVC中页面强类型的个人理解
进入ASP.NET MVC学习 发现很多和winfrom不同的东西,但是利用的C#语言还是没有变化,更多的是利用了新的语言,html jquery ajax.....唉 心累,一本书一本书看的去 看完 ...
- Linux 中Ctrl + s 的作用
在Linux下使用vim编辑程序时,常常会习惯性的按下Ctrl + s保存文件内容.殊不知,这一按不紧,整个终端再也不响应了. 事实上Ctrl + s在终端下是有特殊用途的,那就是暂停该终端,这个功能 ...
- RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2->新增“行政区域管理”,同时大批量树采用异步加载
行政区划:简称政区,是国家为了进行分级管理而实行的区域划分.中国现行的行政区划实行如下原则:1.全国分为省.自治区.直辖市:2.省.自治区分为自治州.县.自治县.市:3.自治州分为县.自治县.市:4. ...
- Mac终端工具item2实现覆盖在屏幕上透明效果
1.去官网安装item2: https://www.iterm2.com/downloads.html 2.打开preferences 3.到keys配置: 4.点击上图展示的Create a Ded ...
- [前端]AngularJS 簡易物件修改入門
各位好,今天要來介紹如何簡單的修改網站上AngularJS相關Application的內容 進而做到某些效果.(警告!所有的Web Application都應該在後端加上相關驗證) 透過本篇你可以簡單 ...
- 使用NOPI写入Excel基础代码
using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.IO; using Sy ...