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
Related
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
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.
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
log(2 different dates):
START TIME BEFORE PARSE: 06/27/2012 09:00
START TIME AFTER PARSE : Thu Mar 06 09:00:00 EET 2014
START TIME BEFORE PARSE: 07/06/2012 09:00
START TIME AFTER PARSE : Thu Jun 07 09:00:00 EEST 2012
code :
DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date date = sdf.parse(time);
System.out.println("TIME BEFORE PARSE: " + time);
System.out.println("TIME AFTER PARSE : " + date);
Why does it mess up the year? How to get it to work?
Because you inverted the month with the date:
dd/MM/yyyy HH:mm
06/27/2012 09:00
There is not 27th month in a year.
The month in the first example is 27 which isn't valid in any calendar I'm aware of. (You probably just got the day/month ordering wrong, either on your input, or in the format you've chosen.)
You use the pattern dd/MM/yyyy to parse the date 06/27/2012. I doubt 27 is a month. The appropriate format is MM/dd/yyyy.
The DateFormat is lenient by default, and will thus consider 27 as a valid month: 2 years + 3 months, so you end up in March, 2 years later.
String time = "06/27/2012 09:00";
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm");
Date date = sdf.parse(time);
System.out.println("TIME BEFORE PARSE: " + time);
System.out.println("TIME AFTER PARSE : " + date);
In your example date format is wrong. You have give "dd/MM/yyyy HH:mm" which should be "MM/dd/yyyy HH:mm"
You have used the pattern dd/MM/YYYY , but you have entered the date as MM/dd/YYYY, causing you this weird behaviour..
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.