当前位置:网站首页>Excel date and number format processing
Excel date and number format processing
2022-06-29 10:46:00 【linsa_ pursuer】
1. Month enumeration class
package com;
public enum MonthEnums {
JAN("Jan"),
FEB("Feb"),
MAR("Mar"),
APR("Apr"),
MAY("May"),
JUN("Jun"),
JUL("Jul"),
AUG("Aug"),
SEP("Sep"),
OCT("Oct"),
NOV("Nov"),
DEC("Dec")
;
private String month;
MonthEnums(String month){
this.month = month;
}
public String getMonth(){
return month;
}
public void setMonth(String month){
this.month = month;
}
}
2.main Method
import com.MonthEnums;
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class ExcelImportFormatUtil {
public static void main(String[] args) {
String month = "Mon Nov 09 00:00:00 CST 2020";//202007 2020 year 7 month 2020/7/31 44043 Mon Nov 09 00:00:00 CST 2020
boolean flag = ExcelImportFormatUtil.getHasDate(month);
if(flag){
String monthTemp = ExcelImportFormatUtil.getDate(month);
boolean flagTemp = ExcelImportFormatUtil.getHasDate(monthTemp);
if(flagTemp){
//" The imported data line month information is not filled in correctly , Do not import !"
}else{
month = monthTemp;
}
}
System.out.println(month);
System.out.println(getDecimals("12.23456", 2, 0));
System.out.println(getDecimals("12.12345", 2, 1));
}
// obtain Excel Text format month 202007
public static boolean getHasDate(String month){
String reg = "[1-9]{1}[0-9]{5}";
if(month.matches(reg)){
if(Integer.parseInt(month.substring(4))>0 && Integer.parseInt(month.substring(4))<13){
return false;
}else{
return true;
}
}else{
return true;
}
}
// obtain Excel Date format month
public static String getDate(String month){
// Process month information ( Consider text and date formatting )2020 year 7 month 2020/7/31 44043 Mon Nov 09 00:00:00 CST 2020
String reg = "[0-9]+";
if(month.matches(reg)){
//44102——》2020/9/28 44075——》2020/9/1 44043——》2020/7/31
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");// Lowercase mm It means minutes
Calendar calendar = Calendar.getInstance();
calendar.set(1900,0,1);
calendar.add(Calendar.DATE,Integer.parseInt(month)-2);
Date date = calendar.getTime();
return sdf.format(date);
}else if(month.contains("/")){
return getYearAndMonth(month.split("/"));
}else if(month.contains(" year ")){
return getYearAndMonth(month.split(" month ")[0].split(" year "));
}else if(month.contains("CST")){
String[] dateArr = month.split(" ");
if(dateArr.length==6){
return getYearAndMonthForDate(dateArr);
}
}
return "";
}
private static String getYearAndMonthForDate(String[] dateArr){
String dateTemp = dateArr[5];
if(MonthEnums.JAN.getMonth().equals(dateArr[1])){
dateTemp = dateTemp + "01";
}else if(MonthEnums.FEB.getMonth().equals(dateArr[1])){
dateTemp = dateTemp + "02";
}else if(MonthEnums.MAR.getMonth().equals(dateArr[1])){
dateTemp = dateTemp + "03";
}else if(MonthEnums.APR.getMonth().equals(dateArr[1])){
dateTemp = dateTemp + "04";
}else if(MonthEnums.MAY.getMonth().equals(dateArr[1])){
dateTemp = dateTemp + "05";
}else if(MonthEnums.JUN.getMonth().equals(dateArr[1])){
dateTemp = dateTemp + "06";
}else if(MonthEnums.JUL.getMonth().equals(dateArr[1])){
dateTemp = dateTemp + "07";
}else if(MonthEnums.AUG.getMonth().equals(dateArr[1])){
dateTemp = dateTemp + "08";
}else if(MonthEnums.SEP.getMonth().equals(dateArr[1])){
dateTemp = dateTemp + "09";
}else if(MonthEnums.OCT.getMonth().equals(dateArr[1])){
dateTemp = dateTemp + "10";
}else if(MonthEnums.NOV.getMonth().equals(dateArr[1])){
dateTemp = dateTemp + "11";
}else if(MonthEnums.DEC.getMonth().equals(dateArr[1])){
dateTemp = dateTemp + "12";
}
return dateTemp;
}
// Verify the text year date format
private static String getYearAndMonth(String[] yearAndMonth){
String regYear = "[1-9]{1}[0-9]{3}";
String regMonth = "[0-9]{1,2}";
if(yearAndMonth.length<2){
return "";
}else{
if(yearAndMonth[0].matches(regYear)){
if(yearAndMonth[1].matches(regMonth)){
if(Integer.parseInt(yearAndMonth[1])>=1 && Integer.parseInt(yearAndMonth[1])<=12){
if(Integer.parseInt(yearAndMonth[1])<10){
return yearAndMonth[0]+0+Integer.parseInt(yearAndMonth[1]);
}else{
return yearAndMonth[0]+Integer.parseInt(yearAndMonth[1]);
}
}else{
return "";
}
}else{
return "";
}
}else{
return "";
}
}
}
/**
* obtain Excel Percentage or number format
* @param type 0- Numbers 1- percentage
*/
public static String getDecimals(String param, int num, int type){
String result = "";
if(StringUtils.isBlank(param) || "NA".equals(param)){
return result;
}
if(param.toString().endsWith("%")){
try {
result = new BigDecimal(param.toString().replace("%",""))
.multiply(new BigDecimal(100)).setScale(num,BigDecimal.ROUND_HALF_UP)
.stripTrailingZeros().toPlainString() + (type==1?"%":"");
}catch(Exception e){
result = "false";
}
}else{
try {
if(type==1){
result = new BigDecimal(param.toString())
.multiply(new BigDecimal(100)).setScale(num,BigDecimal.ROUND_HALF_UP)
.stripTrailingZeros().toPlainString() + (type==1?"%":"");
}else{
result = new BigDecimal(param.toString())
.setScale(num,BigDecimal.ROUND_HALF_UP)
.stripTrailingZeros().toPlainString() + (type==1?"%":"");
}
}catch(Exception e){
result = "false";
}
}
return result;
}
}
3. Display the results
202011
12.23
1212.35%
边栏推荐
猜你喜欢

Comment terminer rapidement une partition de disque

《MongoDB入门教程》第02篇 MongoDB安装

Bug的描述、定级、生命周期

8年打磨,《游戏设计梦工厂》发布史诗级更新!
![[200 opencv routines] 214 Detailed explanation of drawing ellipse parameters](/img/d2/807095d8ebf563915f0674f0992037.png)
[200 opencv routines] 214 Detailed explanation of drawing ellipse parameters

With this tool, automatic identification and verification code is no longer a problem

Analysis of BlockingQueue source code of AQS

BUUCTF--reverse1

《CLR via C#》读书笔记-CLR寄宿与AppDomain

2020-10-17:刷题1
随机推荐
Contents of advanced mathematics
Solve the problem that zxing's QR code contains Chinese garbled code
The difference between & & and &
Dormitory maintenance management system based on stm32+rfid design
反CSRF爆破的三种姿势
Buuctf-- connotative software
如何快速完成磁盤分區
IO流总结
Analysis of reentrantlock source code of AQS
Summary after reading how to read a Book
如何优雅的写 Controller 层代码?
给定两个整形变量的值,将两个值的内容进行交换 (C语言)
std::unique_ PTR < T> and boost:: scoped_ Particularity of PTR < t >
有人遇到FlinkCdc同步MySQL时候出现的这个问题吗?
mysql中的if [not] exists
[200 opencv routines] 214 Detailed explanation of drawing ellipse parameters
软件测试模型(V模型和W模型)
The use and difference of watch listening and computed calculation attributes
Given the values of two integer variables, the contents of the two values are exchanged (C language)
BUUCTF--reverse2