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

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)));

Related

Which is this date format for simpledateformat (android) 2018-10-03T09:00:36.845+0000?

I am getting response string 2018-10-03T09:00:36.845+0000
and i have to parse it for PrettyTime some min ago .
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
its not working, can you tell me Dateformat string to add in SimpleDateFormat ?
Here is My code
:
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
long time = sdf.parse(device_for_bottomviews.get(i).lastupdate).getTime();
PrettyTime prettyTime = new PrettyTime(Locale.getDefault());
String ago = prettyTime.format(new Date(time));
lastupdate.setText(ago);
}
catch (Exception e)
{
e.printStackTrace();
lastupdate.setText(device_for_bottomviews.get(i).lastupdate);
}
exception i am getting
java.lang.IllegalArgumentException
at java.util.Date.parse(Date.java:633)
at java.util.Date.<init>(Date.java:272)
at com.test.busmanagement.MapActivity$DeviceAdapter.getView(MapActivity.java:721)
at android.widget.AbsListView.obtainView(AbsListView.java:2363)
at android.widget.ListView.makeAndAddView(ListView.java:1970)
at android.widget.ListView.fillDown(ListView.java:704)
at android.widget.ListView.fillFromTop(ListView.java:765)
You can use this format as pattern
yyyy-MM-dd'T'HH:mm:ss.SSSX
You don't need to quote the 'zone' part of the date format string, here is a corrected version:
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"

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

Convert a Date dd/MM/yyyy to dd-MM-yyyy in java [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
I'm trying to convert a date in dd-MM-yyyy format from YYYY-MM-dd hh:mm:ss.ms
in java using the below code but m unable to get the desired value
String dob = (new SimpleDateFormat("dd-MM-yyyy")).format(customerEntity.getDob().toString());
customerEntity.getDob().toString is providing me this value 1987-06-12 00:00:00.0
But when i'm parsing it to the String dob it produces 163-06-1987 as the output whereas i want the output like 12-06-1987 .
Any help will be appreciable, thanks well in advance
format method in SimpleDateFormat take a Date as argument and not a String
public static void main(String[] args) {
String dateStr = "29/12/2016";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
Date date = sdf.parse(dateStr);
sdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(sdf.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
Try parsing your string date into a Date first in format it is coming. Post that pass on that Date object to a format in the format you want your output.
As in below :
Date dob = (new SimpleDateFormat("yyyy-MM-dd")).parse("1987-06-12 00:00:00.0");
String dob1 = (new SimpleDateFormat("dd-MM-yyyy")).format(dob);

java Date problems [duplicate]

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.

Categories

Resources