以特定格式解析java中到目前为止的字符串
我正在分析日期,但没有得到正确的结果以特定格式解析java中到目前为止的字符串,java,android,android-emulator,Java,Android,Android Emulator,我正在分析日期,但没有得到正确的结果 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); startDate = dateFormat.parse("2013-05-18"); 获取输出“2013年5月18日星期六00:00:00 IST” 但是我想简单地2013-05-18使用日期格式而不是字符串格式 提前感谢 如果要从字符串“2013-05-18”中提取字符串“2013-05-18”,则根本不需要任何解析。要将
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
startDate = dateFormat.parse("2013-05-18");
获取输出“2013年5月18日星期六00:00:00 IST”
但是我想简单地2013-05-18使用日期格式而不是字符串格式
提前感谢 如果要从字符串“2013-05-18”中提取字符串“2013-05-18”,则根本不需要任何解析。要将日期对象转换为具有给定格式的字符串,请使用SimpleDataFormat.format()
:
日期对象只是一个长值的包装器,该值包含毫秒数。它的
toString()
方法没有使用您用来解析字符串和创建日期对象的模式。我同意@JB nitet
Date now = new Date();
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Format : " + originalFormat.format(now));
输出
Format : 2013-05-18
2013-04-10
2013-05-30
Current Date is between the two dates
SimpleDataFormat是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。它允许格式化(日期->文本)、解析(文本->日期)和规范化
用于日期比较
public class DateComparision
{
public static void main( String[] args )
{
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse("2013-4-10");
Date date2 = sdf.parse("2013-4-30");
System.out.println(sdf.format(date1));
System.out.println(sdf.format(date2));
if(date1.compareTo(date2)>0){
System.out.println("Date1 is after Date2");
}else if(date1.compareTo(date2)<0){
System.out.println("Date1 is before Date2");
}else if(date1.compareTo(date2)==0){
System.out.println("Date1 is equal to Date2");
}else{
System.out.println("How to get here?");
}
}catch(ParseException ex){
ex.printStackTrace();
}
}
}
输出:
2013-08-16
2013-08-15
Date1 is after Date2
编辑2:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse("2013-4-10");
Date date2 = sdf.parse("2013-5-30");
Date currentdate = sdf.parse("2013-5-15");
System.out.println(sdf.format(date1));
System.out.println(sdf.format(date2));
if(date1.compareTo(currentdate) * currentdate.compareTo(date2) > 0)
{
System.out.println("Current Date is between the two dates");
}
输出
Format : 2013-05-18
2013-04-10
2013-05-30
Current Date is between the two dates
以下是SimpleDataFormat的一些有用示例:
public class JavaSimpleDateFormatTest
{
public static void main(String[] args)
{
// (1) get today's date
Date today = Calendar.getInstance().getTime();
// (2) create our date "formatter" (the date format we want)
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-hh.mm.ss");
// (3) create a new String using the date format we want
String folderName = formatter.format(today);
// (4) this prints "Folder Name = 2009-09-06-08.23.23"
System.out.println("Folder Name = " + folderName);
}
}
您可以找到的更多信息请显示显示(打印)startDate.System.out.println(startDate)的代码;但这将返回字符串,我希望输入日期对象。日期没有任何格式。这只是毫秒数。要将其表示为给定格式的字符串,请参见我的答案。您的代码使用了
date.toString()
,因此您还打印了一个字符串。如果您希望date.toString()以首选格式返回日期,则这种情况永远不会发生,因为您必须使用DateFormat来执行此操作。我希望这是date格式,因为我必须比较日期范围,如果(currDate.before(startDate)| | currDate.after(endDate)){}before()和after()不要通过将日期转换为字符串并比较字符串来比较日期。它们只是比较日期对象中包含的毫秒数。它们很好用,别担心。如果你比较两辆车的速度,你不在乎它们是用旋转的圆形面板还是数字显示器来表示速度,是吗?您只关心计数器指示的公里/小时。Date对象也是如此。他们有很多女士,你不在乎这个数字的表达方式。你只是比较一下数字。这就是after()和before()所做的。它将以字符串形式返回,但我希望以日期形式返回。。。!!对不起,这里有一个很好的例子来说明字符串到日期:实际上我必须比较日期如果(currDate.before(startDate)| | currDate.before(endDate)){}@NarendraSinghRajpoot进行日期比较您可以检查上面的内容谢谢您的回复,但我想检查日期是否存在于日期范围内。。。!!!我不明白“日期是否存在”是什么意思?实际上我有一个当前日期,我想比较开始日期和结束日期中存在的日期,这就是为什么我需要正确格式的日期格式,如果(currDate.before(startDate)| currDate.after(endDate)){}