관련 변환 코드

5855 단어
package com.example.guidviewtest;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.annotation.SuppressLint;

@SuppressLint("SimpleDateFormat")
public class Formats {

	private static DecimalFormat doubleFormat = new DecimalFormat("#0.##");
	private static DecimalFormat doubleFormat1 = new DecimalFormat("#0.00");
	private static DecimalFormat moneyFormat = new DecimalFormat("###,###,###,##0.##");

	/** format should not be changed if i start a export/import function **/
	private static DateFormat norDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
	private static DateFormat norDateFormatOld = new SimpleDateFormat("yyyy-MM-dd");
	private static DateFormat norDatetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
		Locale.CHINA);
	private static DateFormat norDatetimeFormatOld = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static DateFormat norDatetimeFormat2 = new SimpleDateFormat("yyyyMMdd", Locale.CHINA);
	private static DateFormat norDatetimeFormatOld2 = new SimpleDateFormat("yyyyMMdd");
	private static DateFormat norDatetimeFormat3 = new SimpleDateFormat("yyyyMMddHHmmss",
		Locale.CHINA);
	private static DateFormat norDatetimeFormatOld3 = new SimpleDateFormat("yyyyMMddHHmmss");
	private static DateFormat norDatetimeFormat4 = new SimpleDateFormat("yyyy-MM-dd HH:mm",
		Locale.CHINA);
	private static DateFormat norDatetimeFormat4Old = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	//private static DateFormat norDatetimeFormatOld4 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	private static DateFormat norDatetimeFormat5 = new SimpleDateFormat("HH:mm:ss");
	private static DecimalFormat norDoubleFormat = new DecimalFormat("#0.###");

	public static String double2String(Double d) {
		return double2String(d == null ? 0D : d);
	}

	public static String double2String(double d) {
		return doubleFormat.format(d);
	}

	public static String double2String1(double d) {
		return doubleFormat1.format(d);
	}

	public static String money2String(double d) {
		return moneyFormat.format(d);
	}

	public static String num2String(long d) {
		return moneyFormat.format(d);
	}

	public static double string2Double(String d) {
		try {
			return doubleFormat.parse(d).doubleValue();
		} catch (ParseException e) {
			return 0D;
		}
	}
	
	public static double string2Double1(String d) {
		try {
			return doubleFormat1.parse(d).doubleValue();
		} catch (ParseException e) {
			return 0D;
		}
	}

	public static String normalizeDouble2String(Double d) {
		return normalizeDouble2String(d == null ? 0D : d);
	}

	public static String normalizeDouble2String(double d) {
		return norDoubleFormat.format(d);
	}

	public static double normalizeString2Double(String d) throws ParseException {
		return norDoubleFormat.parse(d).doubleValue();

	}

	public static String normalizeDate2String(Date date) {
		return norDateFormat.format(date);
	}
	
	/**
	 * 
	 * @param preOrNextData 1     ,0    ,-1     
	 * @return
	 */
	public static String getpreOrNextDate(Date date, int preOrNextDate){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE,preOrNextDate);
		Date dd = cal.getTime();
		return norDateFormat.format(dd);
	}

	public static Date normalizeString2Date(String date) throws ParseException {
		try {
			return norDateFormat.parse(date);
		} catch (ParseException x) {
			return norDateFormatOld.parse(date);
		}
	}

	public static String normalizeDatetime2String(Date date) {
		return norDatetimeFormat.format(date);
	}

	public static Date normalizeString2Datetime(String date) throws ParseException {
		try {
			return norDatetimeFormat.parse(date);
		} catch (ParseException x) {
			return norDatetimeFormatOld.parse(date);
		}
	}

	public static String normalizeDatetime2String2(Date date) {
		return norDatetimeFormat2.format(date);
	}

	public static Date normalizeString2Datetime2(String date) throws ParseException {
		try {
			return norDatetimeFormat2.parse(date);
		} catch (ParseException x) {
			return norDatetimeFormatOld2.parse(date);
		}
	}

	public static String normalizeDatetime2String3(Date date) {
		return norDatetimeFormat3.format(date);
	}

	public static Date normalizeString2Datetime3(String date) throws ParseException {
		try {
			return norDatetimeFormat3.parse(date);
		} catch (ParseException x) {
			return norDatetimeFormatOld3.parse(date);
		}
	}

	public static String normalizeDatetime2String4(Date date) {
		return norDatetimeFormat4.format(date);
	}
	
	public static Date normalizeDatetime2String4(String date) throws ParseException {
		try {
			return norDatetimeFormat4.parse(date);
		} catch (ParseException e) {
			return norDatetimeFormat4Old.parse(date);
		}
	}

	public static String normalizeDatetime2String5(Date date) {
		return norDatetimeFormat5.format(date);
	}
	
	public static void main(String[] args){
		System.out.println(double2String(5.38239));
		System.out.println(double2String(5));
		System.out.println(double2String(1437));
	}
	
	/** 
	 *    yyyyMMdd       yyyy-MM-dd  
	 */
	public static Date strToDateTime(String arg0) {
		Date resultDate = null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		try {
			resultDate = simpleDateFormat.parse(arg0);
			return resultDate;
		} catch (Exception e) {
			return resultDate;
		}
	}

	public static String dateTimeToStr(Date arg0) {
		String resultStr;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		resultStr = simpleDateFormat.format(arg0);
		return resultStr;
	}
}

좋은 웹페이지 즐겨찾기