- 浏览: 1058402 次
- 性别:
- 来自: 南昌
文章分类
- 全部博客 (276)
- 生活 (1)
- 代码之美 (22)
- Media (7)
- Android Widget (3)
- Android Intent (1)
- Android Activity (4)
- UI event handle--UI事件处理机制 (2)
- Java基础知识 (12)
- android Databases (5)
- Android 系统知识 (70)
- 平常遇到的问题与解决方法 (38)
- Android TextView/EditView (2)
- Thinking Java (1)
- android webkit (6)
- JSON (1)
- XML (4)
- HTTP (1)
- Google Weather API (1)
- android 2.3 NFC (10)
- android app (20)
- android framework (7)
- C++ (2)
- android System (5)
- Pthread (1)
- Wifi (8)
- Unix/Linux C (8)
- Android 4.0 (1)
- Mail (1)
- Smack 源码学习 (4)
- iOS (4)
- Android (1)
- git (1)
- Gallery3d (2)
- React-Natice (1)
最新评论
-
dd18349182956:
你是用的smack哪个版本?我用的smack4.1.3和sma ...
关于socket长连接的心跳包 -
xukaiyin:
全英文
getApplicationContext()与this,getBaseContext() -
裂风矢:
...
<category android:name="android.intent.category.DEFAULT" /> 惹的祸 -
xanthodont:
mark一下
XMPP——Smack -
Evilover3:
mark一下,学习了
XMPP——Smack
摘自:http://hi.baidu.com/banseon/blog/item/8a45c8ed09396b4e79f05579.html
System.out.printf
The first way you'll learn to work with the Formatter is not to interact with it directly, but instead to use the new PrintStream method printf. You're probably most familiar with PrintStream from doing System.out.println calls. Here is a simple example of the new printf method:
String initials = "jjl";
String comment = "just because";
System.out.printf("reason: %s (noted by %s)", comment, initials);
The example prints the following output on the console:
reason: just because (noted by jjl)
In the example code, you are passing printf a format string that contains embedded format specifiers. You also pass it a list of arguments, each of which corresponds to a format specifier. There are two format specifiers in the example: %s and %s. The first corresponds to the comment argument, and the second corresponds to the initials argument.
A % in a format string designates the start of a format specifier. The end of a format specifier is triggered by one of many possible characters known as conversions. In the %s format specifier, the lowercase s indicates a String conversion. A string conversion means that Java calls toString on the corresponding argument, and substitutes the result for the format specifier.
String.format
If you only want to obtain a formatted string, but not print it, you can use the static method format on the String class. Here's an example that also demonstrates a few numeric conversions:
int a = 65;
String s =
String.format("char: %c integral: %d octal: %o hex: %x %n",
a, a, a, a);
The %n at the end of the format string indicates a platform-specific line separator. When printed, the String s looks like this:
char: A integral: 65 octal: 101 hex: 41
Numeric conversions also support flags for padding, grouping, justification, and sign.
Note: There are four arguments, each using the reference a. Were you to supply only three, or were one of the arguments an invalid type (for example, a string), the format method would have generated one of a few runtime exceptions. This is unlike C, which ignores any formatting problems.
If you want to use the same argument more than once against a format string, you can use a shortcut:
String s = String.format("char: %c integral:%<d octal: %<o hex: %<x %n", a);
The a reference is specified only once. The less-than sign (<) in a format specifier means that it should use the same argument as the last format specifier.
Dates
Formatter provides an extensive number of date-related conversions. The following code:
String.format("%1$td %1$tb %1$ty", new Date())
produces a string with the value:
26 Feb 04
The 1$ in each format specifier is another way to designate which argument to use. In this example, all three format specifiers target the first argument.
The last part of each format specifier is how you do date conversions?using a two-character sequence starting with a t. The second character indicates the date part to convert. For example, td in the first specifier converts the day of the month.
java.util.Formatter
The Formatter class is at the core of the new formatting capability. It fully supports internationalization by letting you pass a Locale to the constructor; the other formatting methods (e.g. String.format) also allow this.
Formatter also lets you pass an Appendable object. This is a new interface that defines append methods so that the formatter can store its results in a text collector such as a stream object. Sun has modified all relevant Java classes, such as StringBuffer and PrintStream, to implement this interface. Here's how you might use it:
double avogadro = 6.0e23;
StringBuffer buffer = new StringBuffer();
Formatter formatter = new Formatter(buffer, Locale.US);
formatter.format("avogadro's number: %e %n", avogadro);
formatter.format("base of the natural log: %e %n", Math.E);
System.out.println(buffer.toString());
The corresponding output:
[pre]avogadro's number: 6.000000e+23
base of the natural log: 2.718282e+00[/pre]
System.out.printf
The first way you'll learn to work with the Formatter is not to interact with it directly, but instead to use the new PrintStream method printf. You're probably most familiar with PrintStream from doing System.out.println calls. Here is a simple example of the new printf method:
String initials = "jjl";
String comment = "just because";
System.out.printf("reason: %s (noted by %s)", comment, initials);
The example prints the following output on the console:
reason: just because (noted by jjl)
In the example code, you are passing printf a format string that contains embedded format specifiers. You also pass it a list of arguments, each of which corresponds to a format specifier. There are two format specifiers in the example: %s and %s. The first corresponds to the comment argument, and the second corresponds to the initials argument.
A % in a format string designates the start of a format specifier. The end of a format specifier is triggered by one of many possible characters known as conversions. In the %s format specifier, the lowercase s indicates a String conversion. A string conversion means that Java calls toString on the corresponding argument, and substitutes the result for the format specifier.
String.format
If you only want to obtain a formatted string, but not print it, you can use the static method format on the String class. Here's an example that also demonstrates a few numeric conversions:
int a = 65;
String s =
String.format("char: %c integral: %d octal: %o hex: %x %n",
a, a, a, a);
The %n at the end of the format string indicates a platform-specific line separator. When printed, the String s looks like this:
char: A integral: 65 octal: 101 hex: 41
Numeric conversions also support flags for padding, grouping, justification, and sign.
Note: There are four arguments, each using the reference a. Were you to supply only three, or were one of the arguments an invalid type (for example, a string), the format method would have generated one of a few runtime exceptions. This is unlike C, which ignores any formatting problems.
If you want to use the same argument more than once against a format string, you can use a shortcut:
String s = String.format("char: %c integral:%<d octal: %<o hex: %<x %n", a);
The a reference is specified only once. The less-than sign (<) in a format specifier means that it should use the same argument as the last format specifier.
Dates
Formatter provides an extensive number of date-related conversions. The following code:
String.format("%1$td %1$tb %1$ty", new Date())
produces a string with the value:
26 Feb 04
The 1$ in each format specifier is another way to designate which argument to use. In this example, all three format specifiers target the first argument.
The last part of each format specifier is how you do date conversions?using a two-character sequence starting with a t. The second character indicates the date part to convert. For example, td in the first specifier converts the day of the month.
java.util.Formatter
The Formatter class is at the core of the new formatting capability. It fully supports internationalization by letting you pass a Locale to the constructor; the other formatting methods (e.g. String.format) also allow this.
Formatter also lets you pass an Appendable object. This is a new interface that defines append methods so that the formatter can store its results in a text collector such as a stream object. Sun has modified all relevant Java classes, such as StringBuffer and PrintStream, to implement this interface. Here's how you might use it:
double avogadro = 6.0e23;
StringBuffer buffer = new StringBuffer();
Formatter formatter = new Formatter(buffer, Locale.US);
formatter.format("avogadro's number: %e %n", avogadro);
formatter.format("base of the natural log: %e %n", Math.E);
System.out.println(buffer.toString());
The corresponding output:
[pre]avogadro's number: 6.000000e+23
base of the natural log: 2.718282e+00[/pre]
发表评论
-
ContentProvider --Call方法
2018-04-03 16:43 3741很早之前接触ContentProvider知道它提供的方法就是 ... -
android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a read
2015-11-27 14:50 2920android.database.sqlite.SQLiteR ... -
形成一个Resource Uri
2011-02-28 09:51 2954形成一个Resource Uri: public Uri ... -
getStringArray
2011-02-26 11:25 4206将String集放在String的resource里面:(Qu ... -
如何让你的list不被修改
2011-02-25 16:52 1463今天在看QuickSearchBox的源码:发现了可以对lis ... -
按Back key把Activity关掉即使有键盘的时候
2011-02-21 10:38 3075一般情况下,当键盘显示的时候,你按下back只会将键盘关掉,但 ... -
R.bool.use_32bit
2011-02-09 16:49 1947很久没有写新的文章,自己懒了吧,没了动力。今天还是耐住了惰性, ... -
让class只有一个实例的例子
2010-10-25 16:49 1796最近在学习android 的webkit,看到一些自己认为是好 ... -
滚动 TextView
2010-04-16 11:39 1287http://bbs.lupa.gov.cn/home/spa ... -
查询Internal 或External的容量
2010-04-14 21:04 1222我们可通过StatF来查询Internal或External的 ... -
关闭android程序
2010-03-17 17:17 1639转自:http://mingkg21.iteye.com/bl ... -
自定义android Intent Action与继承TextView形成一个动态改变text的View
2010-03-12 17:35 4690//Activity package com.android. ... -
获得手机系统设置的区域
2010-03-09 19:45 2005通过Locate这个类,我们可以获得手机系统设置的区域: ja ... -
关于android获得图片的总结
2010-03-08 14:50 63521,已将图片保存到drawable目录下 [color=cya ... -
android 视频的缩图制作
2010-03-05 15:46 2805在Gallery 这支ap里我们可 ... -
RandomAccessFile
2010-03-05 15:00 4032随机访问类(RandomAccessFile) - [] 输 ... -
Java iterator
2010-02-27 11:29 1474摘自[url]http://hi.baidu.com/heyi ... -
检查盘volumn的容量
2010-02-26 10:57 1394通过StatFs可以获得Filesystem 的状态 Str ... -
检查盘volumn是否可以写入
2010-02-26 10:44 956通过创建一个临时文件来确定是否盘可写。注意不要把文件放在根目录 ... -
Thread
2010-02-26 10:08 1878android 对UI的操作只能在UI线程(一般是在Activ ...
相关推荐
NULL 博文链接:https://vernonchen163.iteye.com/blog/2178868
用法编译: mvn compile 跑步: mvn exec:java -Dexec.mainClass=me.tomassetti.javaformatter.JavaFormatter执照Apache许可2.0依存关系该工具基于 ,Maven会为您提供。 在我将它们发布到Maven Central之前,您需要...
代码格式化配置文件
NULL 博文链接:https://itjiehun.iteye.com/blog/1292545
NULL 博文链接:https://xm-koma.iteye.com/blog/2103643
maven-java-formatter-plugin-0.3.jar
code stytle code formatter
在网上找到了这个formatter,还挺好用的,推荐给大家, 对eclipse格式化你又有了新的选择了..................
maven-java-formatter-plugin-0.4.jar
maven-java-formatter-plugin-0.4-sources.jar
HelloWorldApp.java 第一个用Java开发的应用程序。 firstApplet.java 第一个用Java开发的Applet小程序。 firstApplet.htm 用来装载Applet的网页文件 第2章 示例描述:本章介绍开发Java的基础语法知识。 ...
Java Formatter Debug JDT Developers PHP Development Tools General Fixed support for ASP tags Editor Formatter Debugger Platform Windows MacOS GTK3 Editors Dark Theme Other Open Source Projects Notices
Java类Formatter解析 Formatter是Java中的一个强大类库,它提供了强大且灵活的格式化功能,主要用于文本输出方面,例如数字、日期、金额等。以下是Formatter类的知识点总结: 1. Formatter类的功能:Formatter类...
eclipse formatter和codetemplates java代码编写规范 eclipse formatter和codetemplates java代码编写规范
Java代码的格式化,导入到IDE中,Java Formatter
maven-java-formatter-plugin-0.3.1.jar
<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter" /> <!-- 格式化XML代码 --> value="org.mybatis.generator.api.dom.DefaultXmlFormatter" />
maven-java-formatter-plugin-0.2.0.jar
NULL 博文链接:https://zaq369cde.iteye.com/blog/1908490