JAVA 中获取时间怎么格式化?

Python010

JAVA 中获取时间怎么格式化?,第1张

时间格式化输出主要有两种方式,代码如下:

//使用Calendar

Calendar now = Calendar.getInstance();

System.out.println("年:" + now.get(Calendar.YEAR));

System.out.println("月:" + (now.get(Calendar.MONTH) + 1));

System.out.println("日:" + now.get(Calendar.DAY_OF_MONTH));

System.out.println("时:" + now.get(Calendar.HOUR_OF_DAY));

System.out.println("分:" + now.get(Calendar.MINUTE));

ystem.out.println("秒:" + now.get(Calendar.SECOND));

//使用Date

Date d = new Date();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

System.out.println("当前时间:" + sdf.format(d));

扩展资料

JAVA中获取当前系统时间。

import java.util.Date

import java.text.SimpleDateFormat

public class NowString {

public static void main(String[] args) {

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")//设置日期格式

System.out.println(df.format(new Date()))// new Date()为获取当前系统时间

}

}

参考资料来源:百度百科:Java

楼主你好!给你写了个测试类希望能帮助你。这两个个方法只需要传入你要格式话的数据,就可以返回你想要的结果了。 package com.linepublic class T9 {

/**

* <b>格式化一般数据为财务格式,eg:123,456,789.00</b>

*

* @param source

*String

* @return String

*/

public static String getCaiWuData(String source) {

StringBuffer str = new StringBuffer("")

if (source != null &&!source.equals("") &&source.length() >0

&&!source.equals("null")) {

if (source.lastIndexOf(",") >0) {

source =formatStr(source)

}

int dotIndex = 0

if (source.indexOf(".") <0) {

source += ".00"

}

dotIndex = source.indexOf(".")

int index = 0

String opt = ""

opt = source.substring(0, 1)

if (opt.equals("-")) {

source = source.substring(1)

str.append("-")

dotIndex = source.indexOf(".")

}

if (dotIndex <3) {

index += 1

str.append(source.substring(0, dotIndex))

}

if (dotIndex % 3 == 0) {

index += dotIndex / 3

} else {

index += (dotIndex - dotIndex % 3) / 3

}

if (index >0 &&dotIndex >= 3) {

for (int i = indexi >0i--) {

if (i == index) {

str.append(source.substring(0, dotIndex - i * 3))

}

if (dotIndex - i * 3 >0) {

str.append(",")

}

if (i >= 1) {

str.append(source.substring(dotIndex - i * 3, dotIndex

- (i - 1) * 3))

}

}

}

str.append(source.substring(dotIndex))

}

if (source.length() - source.lastIndexOf(".") <3) {

str.append("0")

}

int dot_index = str.toString().indexOf(".") + 2

int str_len = str.toString().length()

char[] strArr = str.toString().toCharArray()

StringBuffer rev = new StringBuffer()

for (int i = str_len - 1i >0i--) {// 除去尾数0,小数点后保留2位

if (i >dot_index

&&Integer.parseInt(new Character(strArr[i]).toString()) >0) {

rev.append(str.toString().substring(0, i + 1))

break

} else if (i == dot_index &&(int) strArr[i] >= 0) {

rev.append(str.toString().substring(0, dot_index + 1))

break

}

}

return rev.toString()

}

/**

* <b>格式化财务数据为一般字符串,eg:123456789.00</b>

*

* @param source

*String

* @return String

*/

public static String formatStr(String source) {

StringBuffer str = new StringBuffer("")

if (source != null &&!source.equals("") &&source.length() >0

&&!source.equals("null")) {

String temp = source.substring(0, 1)

if (temp.equals("-")) {

source = source.substring(1)

str.append("-")

}

String[] myarr = source.split(",")

int lastIndex = source.lastIndexOf(",")

if (lastIndex >0) {

for (int i = 0i <myarr.lengthi++) {

str.append(myarr[i])

}

}

if (source.lastIndexOf(",") <0) {

str.append(source)

}

if (source.lastIndexOf(".") <0) {

str.append(".00")

}

if (source.length() - source.lastIndexOf(".") <3

&&!"0".equals(source)) {

str.append("0")

}

} else {

return (str.append("0.00").toString())

}

return str.toString()

}

/**

* @param args

*/

public static void main(String[] args) {

T9 t=new T9()

System.out.println(t.getCaiWuData("1231313"))

System.out.println(t.formatStr("1,231,313.00"))

}}

Format 是一个用于格式化语言环境敏感的信息(如日期、消息和数字)的抽象基类。

Format 定义了编程接口,用于将语言环境敏感的对象格式化为 String(使用

format 方法)和将 String 重新解析为对象(使用

parseObject 方法)。

通常,一个 format 的 parseObject 方法必须能解析任何由其 format 方法格式化的字符串。不过,也可能存在不能解析的异常情况。例如,format 方法可能创建中间无分隔符的两个相邻整数,在这种情况下,parseObject 无法判断哪个数字属于哪个数。子类

Java 平台为格式化日期、消息和数字分别提供了三个特殊的 Format 的子类:DateFormat、MessageFormat 和

NumberFormat。

具体的子类必须实现三个方法:

format(Object obj, StringBuffer toAppendTo, FieldPosition pos) formatToCharacterIterator(Object obj) parseObject(String source, ParsePosition pos)

这些常规方法允许对对象进行多态解析和格式化,还可以被使用(如被 MessageFormat 使用)。子类通常也为特定的输入类型提供了另外的 format 方法,也为特定的结果类型提供了 parse 方法。当在输入文本的开始没有任何所需格式的文本时,则任何不带 ParsePosition 参数的 parse 方法都应该抛出 ParseException。

大多数子类还将实现以下工厂方法:

getInstance 获取一个适合于当前语言环境的有用的格式对象 getInstance(Locale) 获取一个适合于指定语言环境的有用的格式对象。