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
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 have wierdo problem with timestamp in Java/Android
Date inputDate = null;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd/MM/yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Log.e("DateH:D", hour+" "+day);
try {
inputDate = sdf.parse(hour + " " + day);
Date currentDate = new Date();
Log.e("InputDate", inputDate.toString());
Log.e("InputDate",inputDate.getTime()+"");
Log.e("CurrentDate", currentDate.toString());
Log.e("CurrentDate",currentDate.getTime()+"");
if (!inputDate.after(currentDate) ){
//TODO change this string
hourField.setError("Date from past");
return false;
}
} catch (ParseException e) {
Log.e("DateParser" , e.getLocalizedMessage);
return false;
}
Example output for this is:
DateH:D: 12:33 15/09/16
E/InputDate: Tue Sep 15 14:33:00 CET 16
E/InputDate: -61640134020000
E/InputDate: Tue Sep 15 14:33:00 CET 16
E/CurrentDate: Thu Sep 15 11:38:43 CEST 2016
E/CurrentDate: 1473932323198
So non-timestamp representation of date is correct but timestamp is wrong. How it's possible? What i'm doing wrong?
Use yy to parse 2-digit years.
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd/MM/yy");
The getTime method of Date:
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
represented by this Date object.
Since the year you specified is year 16, the negative result makes sense.
The starting date for epoch milis is Thu, 01 Jan 1970 00:00:00 GMT. Any datetime before that is negative in epoch milis.
This question already has answers here:
Java String to Date, ParseException
(4 answers)
Closed 7 years ago.
I have a date string as
"Wed Jul 01 08:16:13 PDT 2015"
I am trying to parse it with this SimpleDateFormat
SimpleDateFormat valueDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
this way:
Date parsedDate1 = valueDateFormat.parse("Wed Jul 01 08:16:13 PDT 2015");
It is giving me parse error as:
java.text.ParseException: Unparseable date: "Wed Jul 01 08:16:13 PDT 2015" (at offset 0)
How can I get a date in above simple date format from the string
Try this:
DateFormat originalFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy");
DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = originalFormat.parse("Wed Jul 01 08:16:13 PDT 2015");
String formattedDate = targetFormat.format(date);
System.out.println(formattedDate);
I suspect you don't realy understand the consept of the SimpleDateFormat.
After you define the template with :
SimpleDateFormat valueDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
The valueDateFormat could parse Date object acording to it, it not take just a String you have and convert it. it take Date object.
Date
Your date string ("Wed Jul 01 08:16:13 PDT 2015") doesn't match your pattern ("yyyy-MM-dd hh:mm:ss")
Write correct pattern which matches date string (first goes Day of week, than month in year, etc.)
try this..
public static void main(String[] a)
{
SimpleDateFormat valueDateFormat = new SimpleDateFormat("EEE MMM yy hh:mm:ss");
try {
Date parsedDate1 = valueDateFormat.parse("Wed Jul 01 08:16:13 PDT 2015");
System.out.println(parsedDate1);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
For the formats like this I created a helper method:
public Date parseString(String date) {
String value = date.replaceFirst("\\D+([^\\)]+).+", "$1");
//Timezone could be either positive or negative
String[] timeComponents = value.split("[\\-\\+]");
long time = Long.parseLong(timeComponents[0]);
int timeZoneOffset = Integer.valueOf(timeComponents[1]) * 36000; // (("0100" / 100) * 3600 * 1000)
//If Timezone is negative
if(value.indexOf("-") > 0){
timeZoneOffset *= -1;
}
//Remember that time could be either positive or negative (ie: date before 1/1/1970)
//time += timeZoneOffset;
return new Date(time);
}
It returns date object from Date string so you can format your string like this:
Date date = parseString("Wed Jul 01 08:16:13 PDT 2015");
And after that you can easily format given date variable.
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.
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.