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格式化的更多相关文章

  1. JAVA格式化时间日期

    JAVA格式化时间日期 import java.util.Date; import java.text.DateFormat; /** * 格式化时间类 * DateFormat.FULL = 0 * ...

  2. 7.20.01 java格式化输出 printf 例子

    java格式化输出 printf 例子 importjava.util.Date; publicclassPrintf { publicstaticvoidmain(String[] args) { ...

  3. Java格式化时间

    Java格式化时间 将秒或者毫秒值格式化成指定格式的时间 效果图 工具类 工具类里我只列出了一种格式的格式化方式,可以根据自己的需求,修改"yyyy-MM-dd hh:mm:ss" ...

  4. 【转】java格式化输出 printf 例子

    [转]java格式化输出 printf 例子 转自http://www.cnblogs.com/TankMa/archive/2011/08/20/2146913.html#undefined imp ...

  5. java格式化时间到毫秒

    转自:https://blog.csdn.net/iplayvs2008/article/details/41910835 java格式化时间到毫秒: SimpleDateFormat formatt ...

  6. 8.Java格式化输出

    JAVA中字符串输出格式 1.使用format函数 System.out.format("%d  %f",10,10.5); 2.使用Formatter类 构造函数Formatte ...

  7. java 格式化时间

    java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); form ...

  8. java 格式化日期(DateFormat)

    import java.text.DateFormat; import java.util.Date; /** * 格式化时间类 DateFormat.FULL = 0 * DateFormat.DE ...

  9. Java格式化输出

    Java的格式化输出等同于String.Format,与C有很大的相似,比如 System.out.printf("%8.2f", x);在printf中,可以使用多个参数,例如: ...

  10. java 格式化输出方法

    在javaSE5中推出了printf方法来输出文本到控制台,在java中现在有如下方法可以输出文本: 1.System.out.println(....) //输出并换行 2.System.out.f ...

随机推荐

  1. JVM基础系列第2讲:Java 虚拟机的历史

    说起 Java 虚拟机,许多人就会将其与 HotSpot 虚拟机等同看待.但实际上 Java 虚拟机除了 HotSpot 之外,还有 Sun Classic VM.Exact VM.BEA JRock ...

  2. 听说,霸都.NET技术社区准备搞线下聚会了?

    嗨,你听说了没有?霸都.NET技术社区准备搞线下聚会了! 啥时候的事情啊? 最近才知道的消息啊! 那你是从哪里知道的消息呢? .NET Core项目实战交流群(637326624)啊! 那这次合肥.N ...

  3. Python爬虫入门教程 32-100 B站博人传评论数据抓取 scrapy

    1. B站博人传评论数据爬取简介 今天想了半天不知道抓啥,去B站看跳舞的小姐姐,忽然看到了评论,那就抓取一下B站的评论数据,视频动画那么多,也不知道抓取哪个,选了一个博人传跟火影相关的,抓取看看.网址 ...

  4. 代码实现PHP web服务器下绝对路径到Web网址的转换

    代码实现PHP web服务器下绝对路径到Web网址的转换 代码中用到几个系统变量或常量: 1. 路径中各目录分隔字符 DIRECTORY_SEPARATOR(常量),Windows下DIRECTORY ...

  5. ConcurrentModificationException 异常处理

    在工作中碰到有个异常:java.util.ConcurrentModificationException 腾讯bugly工具给出的解决方案和说明如下:该异常表示迭代器迭代过程中,迭代的对象发生了改变, ...

  6. Chapter 4 Invitations——27

    "Myself, obviously." He enunciated every syllable, as if he were talking to someone mental ...

  7. [ SSH框架 ] Spring框架学习之一

    一.Spring概述 1.1 什么是Spring Spring是一个开源框架, Spring是于2003年兴起的一个轻量级的Java开发框架,由 Rod Johnson在其著作 Expert One- ...

  8. 支持异步写入的日志类,支持Framework2.0

    因为工作需要需要在XP上运行一个C#编写的Winform插件,我就用Framework2.0,因为存在接口交互所以想保留交易过程的入参出参. 考虑到插件本身实施的因素,就没有使用Log4.NLog等成 ...

  9. JavaScript的事件及异常捕获

    事件处理 [onClick]单击事件.[onMouseOver]鼠标经过事件.[onMouseOut]鼠标移出事件.[onChange]文本内容改变事件.[onSelect]文本被框选事件.[onFo ...

  10. 第46章 发现端点(Discovery Endpoint) - Identity Server 4 中文文档(v1.0.0)

    发现端点可用于检索有关IdentityServer的元数据 - 它返回发布者名称,密钥材料,支持的范围等信息.有关详细信息,请参阅规范. 发现端点可通过/.well-known/openid-conf ...