我正在制作一个日期过滤器,我为其创建了一个自定义方法,以以特定日期格式解析日期。我有两种格式DD MMM Yyyy&YYYY-MM-DD以单个方法为解析并以Yyyy-Mm-DD的格式返回。由于我在两种格式的字符串结构处都具有复杂的结构。 问题::当格式在yyyy-mm-dd中时,我从此方法中获得了一个空白字符串。请给我我错了的意见。以下是代码 结果:: current_date =" prev_date =" 2017-04-01" 我被困在这里,请帮助我或告诉我其他通过所需输出获得的方法。 您想要结果格式,例如: 使用: 现在使用: //fetching date from methods
String current_date=CurrentFilterPeriod.dateParsing("2017-04-02");
String prev_date=CurrentFilterPeriod.dateParsing("01 Apr 2017");
//singleton file for date filter method
public class CurrentFilterPeriod {
private static Calendar cal = getInstance();
private static Date current_date = cal.getTime();
//defined formats for date
private static SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
private static SimpleDateFormat formatterString = new SimpleDateFormat("yyyy-MM-dd");
//method for parsing date
public static String dateParsing(String date){
Date newDate;
String returnDate = "";
if (date.equals(formatter.toPattern())){
returnDate=date;
}
Log.e("DB","date===>"+date);
try {
newDate = formatter.parse(date);
Log.e("DB","New Date===>"+newDate);
returnDate=formatterString.format(newDate);
Log.e("DB","returnDate===>"+returnDate);
} catch (ParseException e) {
e.printStackTrace();
}
return returnDate;
}
}
yyyy-mm-dd
。您需要使用formatterString
格式化日期字符串。 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
boolean isValidDate(String input) {
try {
format.parse(input);
return true;
}
catch(ParseException e){
return false;
}
}
//method for parsing date
public static String dateParsing(String date) {
Date newDate;
String returnDate = "";
if (isValidDate(date)) {
returnDate = date;
return returnDate;
} else {
Log.e("DB", "date===>" + date);
try {
newDate = formatter.parse(date);
Log.e("DB", "New Date===>" + newDate);
returnDate = formatterString.format(newDate);
Log.e("DB", "returnDate===>" + returnDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
return returnDate;
}
相关内容
- 如何将Javascript日期字符串转换为Javascript日期对象?
- 将 UTC 日期字符串(如 "2022-07-07T08:17:12.117000")转换为 Kotlin 和 Java 中的本地时区。React 中可用的矩库的替代方案
- 如何根据日期对日期'MMM YYYY'日期字符串进行排序?
- 如何格式化日期字符串与日期-fns没有时区转换?
- Python - Pandas -如何使用日期字符串(字符串格式)下标/切片/选择日期时间索引数据框架
- 如何使用elementtree在python中替换日期字符串和解析xml数据?
- Java短日期字符串转换为ZonedDateTime
- panda将字符串转换为日期(字符串时间为B.E年)
- 从日期字符串中加入连续的日期
- 试图将日期字符串插入到带有pandas数据框架的SQL表中.To_sql函数,但它会重新格式化字符串
- 如何将twitter数据中的日期字符串转换为整数
- 如何将日期字符串转换为时间字符串?
- 无法更新日期字符串对象在mongodb 4.2
- 在shell脚本中从日期字符串格式(2021-09-04T20:02:33,315Z)中提取日期小时、分钟和秒
- 如何解析django模型中包含mm/yyyy的日期字符串
- 我如何验证该请求.body在node JS中使用Joi有一个有效的日期字符串
- 在日期字符串中指定的时区显示日期
- 如何从日期字符串中计算awk内的epoch ?
- Presto SQL -将日期字符串转换为日期格式?
- 如何将Java的日期字符串/对象转换为JFreeChart的Date格式?
最新更新
- OpenGL/glm显示黑屏
- 我收到此警告标志数组索引 4001 已超过数组的末尾(包含 4001 个元素)
- Spring Content 1.2.5 JPA(Postgres) .docx文件突变为zip归档
- 将十进制转换为十六进制,并确保它们是数字
- 为用户在登录页面后添加路由
- 动态变量导致错误,因为它没有正确分配
- 有可能从手机中获得指纹或人脸验证吗?
- File IO From a JAR File
- 如何获得上下文在一个简单的类没有BuildContext在扑动
- 无法向 Tkinter 窗口添加背景
- 如何将RGB图像转换为灰度,但保持一种颜色?——Java
- 我应该在Docker内部还是在虚拟环境中安装气流?
- 属性错误: 'DataFrame'对象没有属性'assign'
- 如何在每次在DirectoryInfo.GetFiles()中加载新文件时执行操作?
- HTML中的Javascript在Angular中不起作用
- 如何在firebase中调度大约每整小时运行一次的函数?
- Uncaught TypeError:不能访问数组上字符串类型的偏移量
- Switch(Select)在TRANSFORM和Select之间工作吗?
- MySQL SELECT查询工作,但具有相同WHERE子句的UPDATE查询不工作
- drupal 10 localhost站点:如何在安装和注销后访问登录页面
- Flutter原生启动画面图像未出现- Flutter (Dart)
- 用另一个远程分支覆盖一个远程分支
- 邮递员说"The route api...could not be found."(拉拉维尔)
- Firebase Messaging项目Google Analytics升级失败
- 在初始页面加载时未应用FacetWP查询顺序数组设置
- GitHub邀请过期吗?
- 使用AWS秘密管理器端点的安全组
- 如何做基于名称和值的点击事件?
- 在容器和主机上创建具有相同Linux用户id的用户
- Firebase身份验证在部署后不工作.扑动Web应用程序显示空白的白色屏幕
热门标签:
javascript python java c# php android html jquery c++ css ios sql mysql arrays asp.net json python-3.x ruby-on-rails .net sql-server django objective-c excel regex ruby linux ajax iphone xml vba spring asp.net-mvc database wordpress string postgresql wpf windows xcode bash git oracle list vb.net multithreading eclipse algorithm macos powershell visual-studio image forms numpy scala function api selenium