This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
String to Date in Different Format in Java
I want to convert String to Date in different Format.
String d = "07:33:01 PM 26/11/2011";
How can i covert it to 26/11-19h30?
DateFormat formatter = new SimpleDateFormat("MM/dd-H\hm");
date = (Date)formatter.parse(d);
You will need to import java.util.* and import java.text.*
String d = "07:33:01 PM 26/11/2011";
Date date = new SimpleDateFormat("d/MM-HH\hmm", Locale.ENGLISH).parse(d);
Have a look at SimpleDateFormat. You can parse to custom date formats using this class.
String d = "07:33:01 PM 26/11/2011";
SimpleDateFormat formatterIn = new SimpleDateFormat("hh:mm:ss a dd/MM/yyyy");
SimpleDateFormat formatterOut = new SimpleDateFormat("dd/MM-HH'h'mm");
Date date = formatterIn.parse(d);
System.out.println(formatterOut.format(date));
It converts to 26/11-19h33, do you want to round to 26/11-19h30?
Related
This question already has answers here:
Difference between java HH:mm and hh:mm on SimpleDateFormat
(5 answers)
java to mysql. I need convert from string parametre to timestamp
(2 answers)
Convert date into AEST using java
(2 answers)
Closed 3 years ago.
I am using below code to parse text to date datatype and get respective long value.
import java.text.DateFormat
import java.text.SimpleDateFormat
String TIMEZONE_DATE_FORMAT = "yyyy-MM-dd'T'hh:mm:ssZ";
DateFormat df = new SimpleDateFormat(TIMEZONE_DATE_FORMAT);
Date date = df.parse("2015-09-21T12:48:00+0000");
date.getTime();
In the above case I get getTime() value as: 1442796480000 which is 12:48 AM(GMT). But I am expecting 12:48 PM as its 24 hr format.
When I use the same code with text date: 2015-09-21T13:48:00+0000, I get 1:48 PM which is correct.
Am I using the wrong date format?
For 24h the pattern should be HH instead hh
Try this:
Date date = new Date();
date.setHours(date.getHours() + 8);
System.out.println(date);
SimpleDateFormat simpDate;
simpDate = new SimpleDateFormat("kk:mm:ss");
System.out.println(simpDate.format(date));
It worked fine for me! :)
This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Java: Date parsing, why do I get an error
(3 answers)
Closed 4 years ago.
Try 1:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+/-HHmm");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(createdDate2);
Try 2:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(createdDate2);
Try 3:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(createdDate2);
Nothing seems to work with this format:
Any help?
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(createdDate2);
This might not exactly be what you want, but if the timezone offset would be written with a colon separator e.g. +00:00 it's the ISO_OFFSET_DATE_TIME
OffsetDateTime d = OffsetDateTime.parse("2018-07-03T01:00:21.000+00:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
System.out.println(d); // 2018-07-03T01:00:21Z
This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 6 years ago.
I will have a String input in the style of:
yyyy-MM-dd'T'HH:mm:ssZ
Is it possible to convert this String into a date, and after, parsing it into a readable dd-MM-yyyy Date Object?
Yes. It can be done in two parts as follows:
Parse your String to Date object
SimpleDateFormat sd1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date dt = sd1.parse(myString);
Format the Date object to desirable format
SimpleDateFormat sd2 = new SimpleDateFormat("yyyy-MM-dd");
String newDate = sd2.format(dt);
System.out.println(newDate);
You will have to use two different SimpleDateFormat since the two date formats are different.
Input:
2015-01-12T10:02:00+0530
Output:
2015-01-12
//Parse the string into a date variable
Date parsedDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(dateString);
//Now reformat it using desired display pattern:
String displayDate = new SimpleDateFormat("dd-MM-yyyy").format(parsedDate);
This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 7 years ago.
From this question and its answers, I tried to convert string to date. But it seems to strange with me.
String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss");
System.out.println(df.parse(test));
It returns
Mon Dec 29 11:56:00 ICT 2014
I have tried with other days but the results is not in the rule (i.e. it have the same distance from the input string and the output date). I am curious. Can anyone explain this for me?
First of all its yyyy not YYYY for year and try the below code to create a Date object from a String.
String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = df.parse(test);
System.out.println(date);
The date format is case-sensitive and therefore the parsing is evaluated differently from what you expected.
Try this:
String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println(df.parse(test));
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to convert java string to Date object
In my Java code I have input String like 05.10.2011 which I need to convert to milliseconds.
So:
String someDate = "05.10.2011";
I have to convert to match milliseconds format.
String someDate = "05.10.2011";
SimpleDateFormat sdf = new SimpleDateFormat("MM.dd.yyyy");
Date date = sdf.parse(someDate);
System.out.println(date.getTime());
You can use Java's SimpleDateFormat to easily convert to a Date instance.
SimpleDateFormat formatter = new SimpleDateFormat("MM.dd.yyyy"); // Month.Day.Year
Date d = formatter.parse(inputString);
long timestamp = d.getTime();
use the simpleDateFormat which takes a string and converts it to Date then call the getTime() to get the date in milliseconds format