java Date problems [duplicate] - java

This question already has answers here:
java date problem in parsing
(4 answers)
Closed 8 years ago.
Hello everybody why the output of this is 0 instead of 9 ? thanks
Date dateNaiss=null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
try {
dateNaiss = sdf.parse("1992-10-10");
} catch (ParseException e1) {
e1.printStackTrace();
}
System.out.println("the mounth of this date is : "+dateNaiss.getMonth());

Because mm is minutes (not month). I believe you wanted
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Replace
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
with
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
mm is for minutes and MM is for months
Also, you shouldn't be using getMonth(), it has been deprecated. Use Calendar class and get Calendar.MONTH from it instead.

Refer to this chart from the documentation:
Notice that m is used for the minute, and M is used for the month.

Related

SimpleDateFormat Java dates [duplicate]

This question already has answers here:
Java SimpleDateFormat Parse Wrong Date
(2 answers)
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Closed 5 years ago.
Any idea what is happening to this code that the dates do not coincide? First prints "20 December 2017" and then "Date with new format:2017-01-01"...
SimpleDateFormat formatter = new SimpleDateFormat("d MMMM YYYY");
SimpleDateFormat formatterNew = new SimpleDateFormat("yyyy-MM-dd");
try{
System.out.println(formatter.format(new Date()));
Date date = formatter.parse("20 December 2017");
System.out.println("Date with new format:" +formatterNew.format(date));
}catch(Exception ex){
ex.printStackTrace();
}
This is related to the use of yyyy vs YYYY. If you change your code to use yyyy it will work.
Here is the explanation of the difference between the two.
Week year

Java SimpleDateFormat: Pattern - ParseException [duplicate]

This question already has answers here:
Datetime parsing error
(2 answers)
Closed 5 years ago.
I am struggling with a date string, I need to parse into the java ‘Date’ object.
Here is what I have got so far:
try {
String value = "2017‎-‎11‎-‎23T14:00:49.184000000Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'";
SimpleDateFormat parser = new SimpleDateFormat(pattern);
Date date = parser.parse(value);
} catch (ParseException e) {e.printStackTrace();}
It currently throws a ParseException “Unparseable date” and I can’t get it to work.
Any help is highly appreciated!
Thanks
Use Instant from java.time package (java 8) instead, it should look like below
String value = "2017-11-23T14:00:49.184000000Z";
Instant instant = Instant.parse(value);
Date date = Date.from(instant);
System.out.println(date);
you can use timeZone as well like this as another solution.
TimeZone tz = TimeZone.getTimeZone("Asia/Calcutta");
Calendar cal = Calendar.getInstance(tz);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'");
sdf.setCalendar(cal);
cal.setTime(sdf.parse("2017-11-23T14:58:00.184000000Z"));
Date date = cal.getTime();
System.out.println(date);

Date format Error(Month conflict) [duplicate]

This question already has answers here:
Getting wrong month when using SimpleDateFormat.parse
(3 answers)
Closed 5 years ago.
I am trying to convert string of format yyyy-mm-dd to dd-MMM-yy. I am getting correct year and days but for month it is showing only jan irrespective of my input. How to fix it?
String input = "2013-09-14";
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-mm-dd");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MMM-yy");
Date date = null;
try {
date = format1.parse(input);
String temp = format2.format(date);
System.out.println(temp);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Output:
14-Jan-13
But I should get:
14-Oct-13
mm is for minute
you need MM or MMM for month.
See SimpleDateFormat for reference.
mm is for minutes. MM is for months.
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
I would suggest latest API LocalDate which not requires try/catch and which is easier to use :
String input = "2013-09-14";
LocalDate inputDate = LocalDate.parse(input, DateTimeFormatter.ISO_DATE);
String format = inputDate.format(DateTimeFormatter.ofPattern("dd-MMM-yy"));
System.out.println(format);
DateTimeFormatter.ISO_DATE is shortcut for DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate inputDate = LocalDate.parse(input); would also work because ISO_DATE the default format
DateTimeFormatter doc (patterns)
The pattern is case sensitive:
MM is month, mm is Minute:
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
For more Information read The javadoc of SimpleDateFormat
personally i used a pattern to do this,
LocalTime w = LocalTime.MIDNIGHT.plus(d);
s = DateTimeFormatter.ofPattern("HH:mm:ss").format(w);
and i advise you to use java 8 date type instead

Java Unparseable date Exception yyyyMMdd h:m a [duplicate]

This question already has answers here:
Why does this SimpleDataFormat parsing fail on Android?
(2 answers)
Closed 6 years ago.
I have this 20160407 4:30 pm data time string and I want to transfer it to timestamp.
Timestamp timestamp = null;
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd h:m a");
//SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
Date parsedDate = dateFormat.parse("20160407 4:30 pm");
timestamp = new java.sql.Timestamp(parsedDate.getTime());
} catch (Exception e) {
e.printStackTrace();
}
I got error:
java.text.ParseException: Unparseable date: "20160407 4:30 pm "
I'm currently in US and your code runs fine if I don't specify a locale. However, the same exception raised when I explicitly changed my locale to China as in Chinese we use '下午' for 'pm' and '上午' for 'am', so if you change 'pm' to '下午' in my code, it will work then.
Locale locale = Locale.CHINA;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd h:m a", locale);
Date parsedDate = dateFormat.parse("20160407 4:30 pm");
So please check what your default locale is by Locale locale = Locale.getDefault().
Also, I would suggest you use the newer Java date and time API too.

Java. Get date and time from string with milliseconds [duplicate]

This question already has answers here:
parsing a date string from FTPClient.getModificationTime()
(2 answers)
Closed 5 years ago.
I try to get date and time from string with milliseconds:
SimpleDateFormat formatDate = new SimpleDateFormat("dd.MM.yyyy HH:mm");
Date modifDate = new Date(Long.parseLong(ftpClient.getModificationTime(file_directory)));
System.out.println(formatDate.format(modifDate));
But I have an exception in second line: java.lang.NumberFormatException: For input string: "213 20140601042221. Where is my mistake and how can I solve it?
Thank you very much!
FTPClient.getModificationTime()
It returns String representing the last file modification time in YYYYMMDDhhmmss format.So if you are trying to parse it to Long than no use of that value.
You can do it like this.
try {
SimpleDateFormat sd=new SimpleDateFormat("YYYYMMDDhhmmss");//From FTPClient
Date date=sd.parse("20140601042221");//Parse String to date
SimpleDateFormat sd2=new SimpleDateFormat("dd.MM.yyyy HH:mm");
System.out.println(sd2.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
well I am not sure what 213 is but 20140601042221 seems to be
YYYYmmddHHMMssSSS
this does not match what you have stated as
new SimpleDateFormat("dd.MM.yyyy HH:mm");
see http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Hint: use the parse method
Try this code :
String dateString=ftpClient.getModificationTime(file_directory));
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
Date modificationDate =
dateFormat.parse(dateString.substring(dateString.indexOf(" ") + 1));
SimpleDateFormat dateOutputFormat=new SimpleDateFormat("dd.MM.yyyy HH:mm");
System.out.println(""+dateOutputFormat.format(modificationDate));
Try getting rid of the 213 and do something like:
SimpleDateFormat formatDate = new SimpleDateFormat("YYYYmmddHHMMssSSS");
System.out.println(formatDate.format(ftpClient.getModificationTime(file_directory)));

Categories

Resources