日期数字格式转化

Posted jiminluo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了日期数字格式转化相关的知识,希望对你有一定的参考价值。

package com.gxnu.study.dateformat;

import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.ChoiceFormat;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.ResourceBundle;

import org.junit.Test;

public class DateFormatEx {
@Test
public void testDateFormat(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
Date date = new Date();
String str = sdf.format(date);
System.out.println(str);

DateFormat df = DateFormat.getDateInstance(DateFormat.LONG,new Locale("fr","FR"));
System.out.println(df.format(date));
}

@Test
public void testL(){
System.getenv().forEach(((k,v)->System.out.println(k+" "+v)));
System.out.println("4656");
System.getProperties().forEach(((k,v)->System.out.println(k+" "+v)));
System.out.println("a");
}

@Test
public void testLocalDate(){
LocalDateTime ldt = LocalDateTime.of(2018, 7,25,10,50);
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME;
System.out.println(dtf.format(ldt));
Calendar calendar = new GregorianCalendar(2018, 7, 25);
System.out.println(calendar.getTime());
}

@Test
public void testNumberFormat(){
NumberFormat mf = NumberFormat.getPercentInstance(new Locale("ar","AR"));
mf.setRoundingMode(RoundingMode.FLOOR);
System.out.println(mf.format(123456.498));
mf.setRoundingMode(RoundingMode.HALF_UP);
System.out.println(mf.format(123456.498));
}

@Test
public void testResourceBundle(){
ResourceBundle rb = ResourceBundle.getBundle("h",new Locale("en","us"));
String str = rb.getString("name");
System.out.println(str);
}

@Test
public void testNumber(){
BigInteger bi = new BigInteger("5485");
BigInteger bi2 = new BigInteger("5655");
BigInteger gcd = bi.gcd(bi2);
System.out.println(gcd);
System.out.println(new BigInteger("10000000000001"));

}

@Test
public void testMessageFormat(){
int planet=7;
String even = ResourceBundle.getBundle("h",new Locale("en","US")).getString("name");

String result = MessageFormat.format("At {1,time} on {1,date},there are {2} on planet {0,number,currency},", planet,new Date(),even);
System.out.println(result);
}

@Test
public void testChoice(){
MessageFormat form = new MessageFormat("The disk "{1}" contains {0}.");
double[] filelimits = {1,2,4,7};
String[] filepart = {"no files","one file","four file","{0,number} files"};
ChoiceFormat fileform = new ChoiceFormat(filelimits,filepart);
form.setFormatByArgumentIndex(0, fileform);

int fileCount = 6;
String diskName = "MyDisk";
Object[] testArgs = {new Long(fileCount),diskName};

System.out.println(form.format(testArgs));
}
}

 

属性文件

技术分享图片

 




























































































以上是关于日期数字格式转化的主要内容,如果未能解决你的问题,请参考以下文章

Java日期类型格式转化

如何把文本转化为数字

如何将Excel日期快速转化为文本格式?

在VB.NET中如何将日期类型转化为String型?

将时间戳转化为日期格式

java中怎么把一个字符串中的数字抽出来转化成日期格式