導航:首頁 > 編程語言 > java轉換工具類

java轉換工具類

發布時間:2022-08-27 08:21:50

A. java的API裡面有沒有像C#那樣通用的Convert工具類來轉換數據類型

各種類型轉換,不一樣


String -> Integer

int a = Integer.parseInt("12345"); //默認是10進制

int a = Integer.parseInt("abcde", 16); //指明是十六進制

B. java 時間轉換工具類 誰有把代碼貼出來謝謝!

提交的時候用SimpleDateFormat類轉換格式:
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
sdf.format(要轉換的Date類型對象);
注意format裡面的參數一定是Date類型的,如果不是請先用sdf.parse(字元串類型值);轉換好後在format也行。

C. java 問題 如何將ppt文件轉為視頻格式文件,最好是非工具類。

一是用office 2010,它可以直接將PPT轉換成AVI。(不知道你的office是不是2010)

二是用屏幕錄像軟體,把PPT錄製成視頻。 如屏幕錄像專家、玩家寶寶等。

三是用轉換軟體:狸窩轉換器,把PPT轉換成視頻格式。 但它需要注冊成VIP會員(也就是付費)後,才可以使用。

其它轉換PPT的軟體,真的是相當的少!(因為office2010已具備這個功能了,所以,沒有再開發這個軟體的了)

D. java中 將含有字母的不定字元串 轉換成定長的數字串,可以用什麼方法或者工具類

publicstaticvoidmain(String[]args){
Stringstr="123a";
try{
Integer.valueOf(str);
System.out.println(str);
}catch(Exceptione){
str="23456";
System.out.println(str);
}
}

E. 在java中怎樣將["你好"]的值轉換成 你好

用JSON包

String str = "[\"你好\"]";
JSON arg0 = JSONArray.fromObject(str);// 為了測試,封裝成JSON對象["你好"]

// 將JSON轉成JAVA對象,需要用List接收
List data = (List) JSONSerializer.toJava(arg0);

System.out.println(data.get(0));

F. java操作excel有多少個工具類

JAVA 通常有兩種方法來操作Excel,分別是POI和JExcelAPI,而且都是開源的。POI是Apace公司開發的,對中文的支持比較弱一些;而JExcelAPI是韓國公司開發的,不僅對中文的支持好,而且由於是純JAVA編寫的,所以可以跨平台操作。本文介紹的也是JExcelAPI的使用方法。

1、環境配置

如下網址,可以下載到API:http://www.andykhan.com/jexcelapi/download.html

下載完成的包解壓之後,可以得到如下幾個重要的文件:

(1)jxl.jar —— JExcelAPI 函數庫;

(2)docs —— 幫助文檔;

(3)src —— 源碼文件夾

將jxl.jar復制到%JAVA_HOME%\jre\ext\文件夾下面,在CLASSPATH變數裡面添加"%JAVA_HOME%\jre\ext",然後就可以調用JExcelAPI了。如果出現編譯報錯「找不到java.jxl包」,則可能是沒有設置成功。這時,如果有Eclipse開發工具,可以在"Build Path"中添加"External Library",找到jxl.jar的路徑,然後就能編譯成功了。

2、Excel基礎操作實例

(1) 創建Excel文件

/**讀取Excel文件的內容
* @param file 待讀取的文件
* @return // 生成Excel的類 */
package createxls;

import java.io.File;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class CreateXLS {
public static void main(String args[]) {
try {
// 打開文件
WritableWorkbook book = Workbook.createWorkbook( new File( " test.xls " ));
// 生成名為「第一頁」的工作表,參數0表示這是第一頁
WritableSheet sheet = book.createSheet( " 第一頁 " , 0 );
// 在Label對象的構造子中指名單元格位置是第一列第一行(0,0)
// 以及單元格內容為test
Label label = new Label( 0 , 0 , " test " );

// 將定義好的單元格添加到工作表中
sheet.addCell(label);

// 寫入數據並關閉文件
book.write();
book.close();

} catch (Exception e) {
System.out.println(e);
}
}
}

(2)讀Excel文件

package readxls;

//讀取Excel的類
import java.io.File;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class ReadXLS {
public static void main(String args[]) {
try {
Workbook book = Workbook.getWorkbook( new File( " test.xls " ));
// 獲得第一個工作表對象
Sheet sheet = book.getSheet( 0 );
// 得到第一列第一行的單元格
Cell cell1 = sheet.getCell( 0 , 0 );
String result = cell1.getContents();
System.out.println(result);
book.close();
} catch (Exception e) {
//System.out.println(e);
e.printStackTrace();
}
}
}

(3)合並單元格、格式化單元格等

//合並單元格並在單元格中輸入內容

package additionalproperty;

import java.io.*;
import jxl.write.*;
import jxl.*;

public class MergeCells {
public static void main(String [] args){
try{
WritableWorkbook book = Workbook.createWorkbook(new File("test.xls"));
WritableSheet sheet = book.createSheet("第一頁", 0);
sheet.mergeCells(3, 3, 6, 6); //合並單元格

//設置填充內容的格式
WritableFont font = new WritableFont(WritableFont.TIMES, 30, WritableFont.BOLD);
WritableCellFormat format = new WritableCellFormat(font);
format.setAlignment(jxl.format.Alignment.CENTRE); //水平居中
format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //垂直居中
format.setBackground(jxl.format.Colour.BLUE); //背景顏色和樣式
format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THICK); //邊框樣式

Label label = new Label(3, 3, "合並", format); //添加內容
sheet.addCell(label);
book.write();
book.close();
}//end try
catch (Exception e){
e.printStackTrace();
}
}
}

(4)添加圖片

JExcelAPI對圖片的操作有限:它不能生成圖表、圖片和宏,但是復制工作表時,這些信息可以保留復制。而且當向工作表中添加圖片時,只能支持PNG格式的圖片。

//在工作表中添加圖片

package handleimage;

import java.io.*;
import jxl.*;
import jxl.write.*;

public class CreateImage {
public static void main(String [] args){
try{
WritableWorkbook book = Workbook.createWorkbook(new File("test.xls"));
WritableSheet sheet = book.createSheet("第一頁", 0);
WritableImage image = new WritableImage(3.5, 3.5, 4.3, 8.7, //定義圖片格式
new File("C:\\Documents and Settings\\Wei Li\\My Documents\\My Pictures\\Water lilies.PNG"));
sheet.addImage(image); //添加圖片

book.write();
book.close();
}//end try
catch (Exception e){ e.printStackTrace(); }
}
}

G. java6 支持使用guava 工具類庫 將list轉換為map嗎

假設有某個類如下
class Movie {

private Integer rank;
private String description;

public Movie(Integer rank, String description) {
super();
this.rank = rank;
this.description = description;
}

public Integer getRank() {
return rank;

H. 急,急急,跪求java十六進制轉換成二進制(要自己寫演算法),再把得到的二進制數取反後,在轉換成十六進制

Java程序:

public class Test29 {
public static void main(String[] args) {
String hex = "12345abcdef67890";
String bin;
bin = Transform.convertHexToBin(hex);
System.out.println(hex + " --> " + bin);

hex = Transform.convertBinToHex(bin);
System.out.println(bin + " --> " + hex);
}
}

class Transform{
private static String[] hexs = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"a", "b", "c", "d", "e", "f"};
private static String[] bins = new String[]{"0000", "0001", "0010", "0011", "0100", "0101",
"0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};

//將十進制數hex轉換為二進制數並返回
public static String convertHexToBin(String hex){
StringBuffer buff = new StringBuffer();
int i;
for(i=0; i<hex.length(); i++){
buff.append(getBin(hex.substring(i,i+1)));
}
return buff.toString();
}

//將二進制數bin轉換為十六進制數並返回
public static String convertBinToHex(String bin){
StringBuffer buff = new StringBuffer(bin);
int i;
if(bin.length()%4 != 0){//左補零
for(i=0; i<(4-bin.length()%4); i++){
buff.insert(0, "0");
}
}
bin = buff.toString();
buff = new StringBuffer();

for(i=0; i<bin.length(); i+=4){
buff.append(getHex(bin.substring(i,i+4)));
}
return buff.toString();
}

//返回十六進制數的二進制形式
private static String getBin(String hex){
int i;
for(i=0; i<hexs.length && !hex.toLowerCase().equals(hexs[i]); i++);
return bins[i];
}

//返回二進制數的十六進制形式
private static String getHex(String bin){
int i;
for(i=0; i<bins.length && !bin.equals(bins[i]); i++);
return hexs[i];
}
}

運行測試:
12345abcdef67890 -->
--> 12345abcdef67890

I. java實現金額轉換,阿拉伯數字的金額轉換成中國傳統的形式

直接通過以下介面類方法實現即可:
import java.math.BigDecimal;
/**
* 金額工具類
*
* @author zn
*
* @Date 2013-2-1
* @Email [email protected]
*/
public class MoneyUtil {

private static final int DFT_SCALE = 2;

/** 大寫數字 */
private static final String[] NUMBERS = { "零", "壹", "貳", "叄", "肆", "伍",
"陸", "柒", "捌", "玖" };
/** 整數部分的單位 */
private static final String[] IUNIT = { "元", "拾", "佰", "仟", "萬", "拾", "佰",
"仟", "億", "拾", "佰", "仟", "萬", "拾", "佰", "仟" };
/** 小數部分的單位 */
private static final String[] DUNIT = { "角", "分", "厘" };

/**
* 得到大寫金額。
*/
public static String toChinese(String str) {
str = str.replaceAll(",", "");// 去掉","
String integerStr;// 整數部分數字
String decimalStr;// 小數部分數字

// 初始化:分離整數部分和小數部分
if (str.indexOf(".") > 0) {
integerStr = str.substring(0, str.indexOf("."));
decimalStr = str.substring(str.indexOf(".") + 1);
} else if (str.indexOf(".") == 0) {
integerStr = "";
decimalStr = str.substring(1);
} else {
integerStr = str;
decimalStr = "";
}
// integerStr去掉首0,不必去掉decimalStr的尾0(超出部分捨去)
if (!integerStr.equals("")) {
integerStr = Long.toString(Long.parseLong(integerStr));
if (integerStr.equals("0")) {
integerStr = "";
}
}
// overflow超出處理能力,直接返回
if (integerStr.length() > IUNIT.length) {
System.out.println(str + ":超出處理能力");
return str;
}

int[] integers = toArray(integerStr);// 整數部分數字
boolean isMust5 = isMust5(integerStr);// 設置萬單位
int[] decimals = toArray(decimalStr);// 小數部分數字
return getChineseInteger(integers, isMust5)
+ getChineseDecimal(decimals);
}

/**
* 整數部分和小數部分轉換為數組,從高位至低位
*/
private static int[] toArray(String number) {
int[] array = new int[number.length()];
for (int i = 0; i < number.length(); i++) {
array[i] = Integer.parseInt(number.substring(i, i + 1));
}
return array;
}

/**
* 得到中文金額的整數部分。
*/
private static String getChineseInteger(int[] integers, boolean isMust5) {
StringBuffer chineseInteger = new StringBuffer("");
int length = integers.length;
for (int i = 0; i < length; i++) {
// 0出現在關鍵位置:1234(萬)5678(億)9012(萬)3456(元)
// 特殊情況:10(拾元、壹拾元、壹拾萬元、拾萬元)
String key = "";
if (integers[i] == 0) {
if ((length - i) == 13)// 萬(億)(必填)
key = IUNIT[4];
else if ((length - i) == 9)// 億(必填)
key = IUNIT[8];
else if ((length - i) == 5 && isMust5)// 萬(不必填)
key = IUNIT[4];
else if ((length - i) == 1)// 元(必填)
key = IUNIT[0];
// 0遇非0時補零,不包含最後一位
if ((length - i) > 1 && integers[i + 1] != 0)
key += NUMBERS[0];
}
chineseInteger.append(integers[i] == 0 ? key
: (NUMBERS[integers[i]] + IUNIT[length - i - 1]));
}
return chineseInteger.toString();
}

/**
* 得到中文金額的小數部分。
*/
private static String getChineseDecimal(int[] decimals) {
StringBuffer chineseDecimal = new StringBuffer("");
for (int i = 0; i < decimals.length; i++) {
// 捨去3位小數之後的
if (i == 3)
break;
chineseDecimal.append(decimals[i] == 0 ? ""
: (NUMBERS[decimals[i]] + DUNIT[i]));
}
return chineseDecimal.toString();
}

/**
* 判斷第5位數字的單位"萬"是否應加。
*/
private static boolean isMust5(String integerStr) {
int length = integerStr.length();
if (length > 4) {
String subInteger = "";
if (length > 8) { // TODO 12-9-17
// 取得從低位數,第5到第8位的字串
subInteger = integerStr.substring(length - 8, length - 4);
} else {
subInteger = integerStr.substring(0, length - 4);
}
return Integer.parseInt(subInteger) > 0;
} else {
return false;
}
}

/**
* BigDecimal 相乘,四捨五入保留0位
*
* @param a
* @param b
* @return a*b
*/
public static BigDecimal mutiply(String a, String b, int roundingMode) {
BigDecimal bd = new BigDecimal(a);
return bd.multiply(new BigDecimal(b)).setScale(DFT_SCALE, roundingMode);
}

/**
* BigDecimal 相除,四捨五入保留兩位
*
* @param a
* @param b
* @return a/b
*/
public static BigDecimal div(String a, String b, int roundingMode) {
BigDecimal decimal1 = new BigDecimal(a);
BigDecimal decimal2 = new BigDecimal(b);
return decimal1.divide(decimal2, DFT_SCALE, roundingMode);
}

/**
* BigDecimal 相加,四捨五入保留兩位
*
* @param a
* @param b
* @return a+b
*/
public static BigDecimal sum(String a, String b, int roundingMode) {
BigDecimal decimal1 = new BigDecimal(a);
BigDecimal decimal2 = new BigDecimal(b);
// DecimalFormat format = new DecimalFormat("#0.00");
return decimal1.add(decimal2).setScale(DFT_SCALE, roundingMode);
}

/**
* BigDecimal 相減,四捨五入保留兩位
*
* @param a
* @param b
* @return a+b
*/
public static BigDecimal sub(String a, String b, int roundingMode) {
BigDecimal decimal1 = new BigDecimal(a);
BigDecimal decimal2 = new BigDecimal(b);
// DecimalFormat format = new DecimalFormat("#0.00");
return decimal1.subtract(decimal2).setScale(DFT_SCALE, roundingMode);
}

/**
* 100.00 為10000
*
* @param a
* @return
*/
public static BigDecimal format(String a, int roundingMode) {
return new BigDecimal(a).multiply(new BigDecimal(100)).setScale(0,
roundingMode);
}

public static void main(String[] args) {
String number = "54452";
System.out.println(number + " " + MoneyUtil.toChinese(number));
number = "30200";
System.out.println(number + " " + MoneyUtil.toChinese(number));
number = "30000.05";
System.out.println(number + " " + MoneyUtil.toChinese(number));
number = "30000.00";
System.out.println(number + " " + MoneyUtil.toChinese(number));
}
}
備註:最後面的main方法是具體的調用。

閱讀全文

與java轉換工具類相關的資料

熱點內容
編譯原理代碼在哪裡運行 瀏覽:584
解密攝影pdf 瀏覽:72
演算法編程中級題目 瀏覽:249
c語言編譯器畢業設計 瀏覽:715
醫保卡申請app哪個好 瀏覽:944
阿里雲伺服器上傳源碼 瀏覽:602
營銷管理科特勒pdf 瀏覽:696
願望清單app哪個好 瀏覽:459
安卓外放聲音怎麼解決 瀏覽:195
脈脈app干什麼用的 瀏覽:360
拽姐是哪個app 瀏覽:860
雲伺服器刪除了還有嗎 瀏覽:234
macbook可以用單片機嘛 瀏覽:309
南陽php招聘 瀏覽:816
去哪裡找按摩師很漂亮的app 瀏覽:821
86x99用簡便演算法計算 瀏覽:833
php截圖flash 瀏覽:276
卸載聯想app哪個好 瀏覽:722
php文字轉圖片 瀏覽:332
豆客後台怎麼加密碼 瀏覽:577