import java.text.ParseException;
創新互聯公司不只是一家網站建設的網絡公司;我們對營銷、技術、服務都有自己獨特見解,公司采取“創意+綜合+營銷”一體化的方式為您提供更專業的服務!我們經歷的每一步也許不一定是最完美的,但每一步都有值得深思的意義。我們珍視每一份信任,關注我們的成都網站建設、做網站質量和服務品質,在得到用戶滿意的同時,也能得到同行業的專業認可,能夠為行業創新發展助力。未來將繼續專注于技術創新,服務升級,滿足企業一站式營銷型網站需求,讓再小的品牌網站制作也能產生價值!
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;
public class Cat {
public static void main(String[] args) {
System.out.print("Please input a date in format yyyy-MM-dd HH:mm:ss: ");
String input = new Scanner(System.in).nextLine().trim();
try {
Calendar cal = Calendar.getInstance();
cal.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(input));
cal.add(Calendar.HOUR, 100);
String date = new SimpleDateFormat("HH:mm:SS yyyy/MM/dd").format(cal.getTime());
System.out.println(date);
} catch (ParseException e) {
System.out.println("日期時間不正確");
}
}
}
------------------測試
Please input a date in format yyyy-MM-dd HH:mm:ss: 2011-6-30 3:10:10
07:10:00 2011/07/04
使用SimpleDateFormat即可,代碼如下
import?java.text.ParseException;
import?java.text.SimpleDateFormat;
import?java.util.Date;
public?class?DateUtil?{
public?static??String?formatDate(Date?date)throws?ParseException{
SimpleDateFormat?sdf?=?new?SimpleDateFormat("yyyy-MM-dd?HH:mm:ss");
return?sdf.format(date);
}
public?static?Date?parse(String?strDate)?throws?ParseException{
SimpleDateFormat?sdf?=?new?SimpleDateFormat("yyyy-MM-dd?HH:mm:ss");
return?sdf.parse(strDate);
}
}
樓主你好!給你寫了個測試類希望能幫助你。這兩個個方法只需要傳入你要格式話的數據,就可以返回你想要的結果了。 package com.line;public 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 = index; i 0; i--) {
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 - 1; i 0; i--) {// 除去尾數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 = 0; i myarr.length; i++) {
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"));
}}
ctrl+shift+f(默認)
可以找在配置里面找到格式化模板,在keys里面設置快捷鍵
在windows-preferences中搜索templates
在windows-preferences中搜索keys
如果是程序實現,里面好像有個標準html流輸出的實現,具體忘記了,自己找找。。。