How to parse a Date+Time String [duplicate] - java

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 8 years ago.
Using java I need to parse this 2014-08-31 13:53:42.0 to 31-AUG-14 01.53.42 PM

Start by doing some research into java.text.SimpleDateFormat which can be used to parse String values of a verity of formats into a java.util.Date.
You can then use another SimpleDateFormat to format the value to the format that you want, for example
try {
String in = "2014-08-31 13:53:42.0";
SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date date = sdfIn.parse(in);
System.out.println(date);
SimpleDateFormat sdfOut = new SimpleDateFormat("dd-MMM-yy hh:mm.ss a");
System.out.println(sdfOut.format(date));
} catch (ParseException ex) {
}
Which outputs
Sun Aug 31 13:53:42 EST 2014
31-Aug-14 01:53.42 PM

Related

Java date format dd.mm.yyyy [duplicate]

This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
Closed 1 year ago.
I have to parse String to Date format(dd.MM.YYYY).
Here is my code:
Date date = null;
try {
date = new SimpleDateFormat("dd.MM.yyyy").parse(regDate);
} catch (Exception e) {
System.out.println(e);
}
System.out.println(date);
But the output is: Tue Oct 12 00:00:00 EEST 2021. How can I change my code to print the date in the required format?
Try By using - or / in place of "."
date = new SimpleDateFormat("dd-MM-yyyy").parse(regDate);
or
date = new SimpleDateFormat("dd/MM/yyyy").parse(regDate);

Converting Java string to Date [duplicate]

This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Closed 4 years ago.
I need to convert a java string in a date format to a new date format to send it to my sql-server. Currently, my Java date is Jul 17 2018 14:22:58, and I need it to look like 2018-07-17 14:22:58. So I created this:
String oldTime = "Jul 17 2018 14:22:58";
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS");
Date formattedDate = sdf.parse(oldTime);
I am getting errors that my oldTime is unparseable. Did I do this wrong?
You need to use a format string that matches your date string:
String oldTime = "Jul 17 2018 14:22:58";
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy HH:mm:ss");

How to display date in dd/mm/yyyy format? [duplicate]

This question already has answers here:
Convert java.util.Date to String
(17 answers)
How can I parse/format dates with LocalDateTime? (Java 8)
(11 answers)
Closed 8 years ago.
I am to new to Java. I want to display date in dd/mm/yyyy format. When I print date using new Date() function it gives me Sting which I don't want. Say I want to print today's date it gives me Sat Jan 24 08:17:41 IST 2015 but I want output as 24/1/2015
Use SimpleDateFormat:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String date = sdf.format(new Date());
There you have one way to do it: http://docs.oracle.com/javase/tutorial/datetime/iso/format.html
So basically you can define the format in an formatter and use that to convert
the date into a string:
try {
DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a");
String out = departure.format(format);
System.out.printf("LEAVING: %s (%s)%n", out, leavingZone);
}
catch (DateTimeException exc) {
System.out.printf("%s can't be formatted!%n", departure);
throw exc;
}

Why SimpleDateFormat does not apply specified date pattern? [duplicate]

This question already has answers here:
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 8 years ago.
I have following code to format a date, but the output does not match with the pattern.
try{
String date = "2014-11-1T12:14:00";
Date convertedDate = new SimpleDateFormat("yyyy-MM-dd").parse(date);
System.err.println(convertedDate.toString());
}catch(ParseException p){
System.err.println(p.getMessage());
}
Output
Sat Nov 01 00:00:00 EST 2014
I think you are questioning why you don't get the time, that's because your format doesn't include it. I suggest you use "yyyy-MM-dd'T'HH:mm:ss" to match your input and "yyyy-MM-dd" when you call DateFormat#format(Date),
try {
String date = "2014-11-1T12:14:00";
Date convertedDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(date);
System.out.println(convertedDate.toString());
// Per your comment you wanted to `format()` it like -
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(convertedDate));
} catch (ParseException p) {
System.err.println(p.getMessage());
}
For what you described below in our conversation, you can simply do this:
System.err.println((new SimpleDateFormat("yyyy-MM-dd")).format(convertedDate));

Android date format not correct? dd/MM/yyyy [duplicate]

This question already has answers here:
How to format date and time in Android?
(26 answers)
Closed 8 years ago.
I am trying to get a date to look like this format: 23/01/2014
This is what I currently have:
String testDate = test.getDate();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(testDate);
} catch (ParseException e) {
e.printStackTrace();
}
Log.i("DATE", convertedDate + "");
My output is: Mon Jul 07 09:40:54 GMT 2014 ?
Try this,
import android.text.format.DateFormat;
String dateString = (String) DateFormat.format("dd-MM-yyyy",new java.util.Date());
Check it with a simple toast message.
Toast.makeText(getActivity(),dateString,Toast.LENGTH_SHORT).show();

Categories

Resources