当前位置:网站首页>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%
边栏推荐
- The product strength is not inferior to that of BYD. Geely Dihao l Raytheon hi · x delivered 10000 units in the first month
- mysql 8.0 一条insert语句的具体执行流程分析(三)
- FreeRTOS porting of official website based on keil5 auto configuring STM32F103 standard library
- Print leap years between 1000 and 2000 (C language)
- The difference between & & and &
- C#中Linq常用用法
- 30岁,女,普通软件测试媛,对职业的迷茫和焦虑
- Is it safe to open a securities account? Is it reliable?
- 《MongoDB入门教程》第02篇 MongoDB安装
- 深入浅出总结Flink运行时架构
猜你喜欢

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

《CLR via C#》读书笔记-单实例应用程序

View CSDN blog rankings

Print prime numbers between 100 and 200 (C language)

BUUCTF--reverse1
![[200 opencv routines] 214 Detailed explanation of drawing ellipse parameters](/img/d2/807095d8ebf563915f0674f0992037.png)
[200 opencv routines] 214 Detailed explanation of drawing ellipse parameters

Buuctf-- happy New Year

Analysis of BlockingQueue source code of AQS

通过Win32API调用另一界面的按钮

全面理解Synchronized
随机推荐
Reprint: five methods to determine whether an object has attributes
基于STM32+RFID设计的宿舍检修管理系统
Reading notes of CLR via C -clr boarding and AppDomain
攻防世界-Re-insfsay
Buuctf-- connotative software
你的项目需要自动化测试吗?
SQL Server 数据库的连接查询
CLR via C reading notes - loading and AppDomain
BUUCTF RE-easyre
【C语言进阶】通讯录实现
mysql中的if [not] exists
美国EB-5移民再现利好,区域中心再授权政策被叫停
Linux下Redis安装及集群搭建
With this tool, automatic identification and verification code is no longer a problem
二叉树
SQL Server 数据库的统计查询
AQS之Atomic详解
C#使用WinExec调用exe程序
如何快速完成磁盘分区
C # use winexec to call exe program