I am new to java. I have a Date that is stored in the variable, pubDate = "2013-09-23"
When I'm executing this
SimpleDateFormat pubSimpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
Date publishDate = pubSimpleDateFormat.parse(pubDate);
I'm getting wrong value : Wed Jan 23 00:09:00 GMT+05:30 2013
Please help me why it so. and help me to solve this.
M is for Month in year while m is for Minute in hour
You should use SimpleDateFormat pubSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String pubDate = "2013-09-23";
SimpleDateFormat pubSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date publishDate = pubSimpleDateFormat.parse(pubDate);
System.out.println(publishDate);
Output :
Mon Sep 23 00:00:00 GMT 2013
Read the section Date and Time Patterns.
Related
I have a given String 2019-04-17 10:00:43+02:00 and want to convert it to something like Mon Apr 15 12:05:47 CEST 2019
I tried:
String date = "2019-04-17T11:02:46+02:00"
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-mm-dd'T'hh:mm:ssX ");
java.util.Date result = formatter.parse(date);
but it gives an Exception like below.
Exception in thread "main" java.text.ParseException: Unparseable date:
"2019-04-17 11:02:46+02:00"
at java.text.DateFormat.parse(Unknown Source)
This is a default ISO format which can be parsed as is with OffsetDateTime:
String date = "2019-04-17T11:02:46+02:00";
OffsetDateTime odt = OffsetDateTime.parse(date);
If you really need a java.util.Date, you can then use:
Date legacyDate = Date.from(odt.toInstant());
the problem with your code is you have put a extra space in the formatter.
String date = "2019-04-17T11:02:46+02:00";
DateFormat format = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm:ssX");
Date rst = format.parse(date);
System.out.println(rst);
output :
Thu Jan 17 14:32:46 IST 2019
Using SimpleDateFormat:
DateFormat df = new SimpleDateFormat("dd-MMMM-YYYY kk:mm:ss.SSS");
Date extractedDate = df.parse(possibleDate);
Input given:
11-May-2017 21:45:33.614
Output data object:
Sun Jan 01 21:45:33 MST 2017
I have tried lots of iterations but it won't pull the month and day.
Use dd-MMM-yyyy kk:mm:ss.SSS as pattern
Example :
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy kk:mm:ss.SSS");
Date extractedDate = df.parse("11-May-2017 21:45:33.614");
System.out.println(extractedDate);
Output :
Thu May 11 21:45:33 BDT 2017
Another thing If you use kk for hour, hour should be represent between 1 to 24. If the hour between 0 to 23 use HH instead of kk
Input date is 2016-01-01, but why output shows 2016/02/01?
String df = "2016-01-01";
String enddate="";
SimpleDateFormat DATE_FORMAT_QUERY = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
Calendar cal=Calendar.getInstance();
String[] dateStr=df.split("-");
int year=Integer.parseInt(dateStr[0]);
int month=Integer.parseInt(dateStr[1]);
int day=Integer.parseInt(dateStr[2]);
cal.set(year,month,day,23, 59,59);
System.out.println(cal.getTime());
enddate=DATE_FORMAT_QUERY.format(cal.getTime());
System.out.println(enddate);
Output:
Mon Feb 01 23:59:59 EST 2016 20160201T235959Z
ANSWER TO YOUR QUESTION
Input date is 2016-01-01, but why output shows 2016/02/01?
Because Calendar::month is 0-based.
month - the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January.
You should use
int month=Integer.parseInt(dateStr[1] - 1);
CORRECT SOLUTION
NEVER parse manually a String containing a Date, better get date with SimpleDateFormat and use it to set Calendar time:
SimpleDateFormat dfo = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal=Calendar.getInstance();
cal.setTime(dfo.parse("2016-01-01"));
OUTPUT:
Fri Jan 01 00:00:00 CET 2016
20160101T000000Z
DateFormat formatter = new SimpleDateFormat("yy-mm-dd");
formatter.setLenient(false);
String[] dateStr = { "2013-12-27", "2013-01-03"};
for (int i = 0; i <= 1; i++) {
Date date = formatter.parse(dateStr[i]);
System.out.println("date is "+date);
}
result :
Sun Jan 27 00:12:00 IST 2013
Thu Jan 03 00:01:00 IST 2013
i am parsing string date in to Date.but it is giving me date Starting with month Jan regardless of what month i am passing to formatter constructor.
The format for your date would be yy-MM-dd. Update your format and check.
mm for minutes
MM for month
Use: "yy-MM-dd"
See here
once Silly Mistake
DateFormat formatter = new SimpleDateFormat("yy-MM-dd");
Format this line in your code
Trying to compare some dates in java but can't get the formatting right, where am i going wrong?
DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
Date date1 = null, date2 = null, today = new Date();
date1 = (Date) df.parse(scan.next());
System.out.println(date1);
System.out.println(today);
if(date1.compareTo(today) < 0){
date1 = null;
System.out.println(start + " is not a valid date.. please try again!");
}
Please enter a start date:
10/04/2011
Mon Jan 10 00:04:00 GMT 2011
Tue Apr 05 22:27:44 BST 2011
I think you need MM, not mm
From the doc:
M Month in year
m Minute in hour
Change line 1 to be:
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
mm in SimpleDateFormat is the minutes. MM is the month. So your input is actaully January 10 2011 at 00:10:00
Check out http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html for abbreviations and javadoc.