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
Related
This question already has answers here:
Change date string format in android
(8 answers)
Date format conversion Android
(9 answers)
Closed 2 years ago.
I get datetime data from soap web service to get string:2020-05-03T00:00:00.
Is there a way to split the string into dd / mm / yyyy, and if it is null then omitted?
I am using a substring(0,10) , but it doesn't work very well.
You can do it this way.
Parse the input string.
LocalDateTime dateTime = LocalDateTime.parse("2020-05-03T00:00:00");
Generate text representing the value of that LocalDateTime object.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String dateTimeString = dateTime.format(formatter);
System.out.println(dateTimeString);
03/05/2020
For early Android before 26, see the ThreeTenABP & ThreeTen-Backport projects, a back-port of most of the java.time functionality.
try with this
try {
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = null;
date = inputFormat.parse("2020-05-03T00:00:00");
String formattedDate = outputFormat.format(date);
System.out.println("coverted: "+formattedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Output: 03-05-2020
You can use java Calendar class to get Date, Month and Year as shown in below
String s = "2020-05-03T00:00:00";
Calendar calendar = new GregorianCalendar();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date dateObj = null;
try {
dateObj = format.parse(s);
calendar.setTime(dateObj);
int date = calendar.get(Calendar.DATE);
int month = calendar.get(Calendar.MONTH) + 1;// As start from 0
int year = calendar.get(Calendar.YEAR);
System.out.println("Formatted Date -> "+ date + "/" + month + "/" +year);
} catch (ParseException e) {
e.printStackTrace();
}
You can do many more thing after converting it to Calendar class object, like hour, minutes, day of week etc
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);
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.
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)));
I am developing an application and I am stuck in converting string like 01/01/2037 01:00:00 AM
to Date
I used
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh.mm.ss.S aa")
Date d = dateFormat.parse(dateString);
but I get an error, any help will be appreciated.
you are converting this 01/01/2037 01:00:00 AM
therefore use
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa")
(more info in [documentation])1
then
Date date = dateFormat.parse("01/01/2037 01:00:00 AM");
keep in mind you have to wrap a try-catch around the parse method.
The problem is that the format you declared is nothing like the String you are trying to parse:
your String uses / to separate day, month, year while in your formatter you use -
your string separates hours with a dot, while in the formatter you use :
you do not have milliseconds in your string while you declared them in the formatter.
The following code should work:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh.mm.ss.S aa");
try {
Date date = dateFormat.parse("01-01-2037 01.00.00.000 AM");
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}