This question already has answers here:
Java string to date conversion
(17 answers)
Closed 9 years ago.
I have a String value which I need to convert into time and save it in the MySQL.[The column's datatype is time in the database table]
The examples of String values that I might encounter to convert into time object are:
String time = "5:32 PM";
String time = "12:00 AM";
String time = "11:43 PM";
I browsed for few examples which pulled the time from the Date object but couldn't find an apt example, such as in this case to convert it from a plain string. The main reason I need to convert it to time is to save it in the mysql.
You can convert it to java.sql.Date as :
String str = "5:32 PM";
DateFormat formatter = new SimpleDateFormat("hh:mm a");
java.util.Date date = (java.util.Date)formatter.parse(str);
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
If you need java.sql.Time , then :
java.sql.Time time = new java.sql.Time(date.getTime());
And then use PreparedStatement#setTime().
Related
This question already has answers here:
Parse String to Date with Different Format in Java
(10 answers)
Closed 4 years ago.
First i have a java Date object lets say
Date firstdate; // its initialized and equal to some date dont worry
with using this code i convert it to dd/MM/yyyy format
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
date.setText(sdf.format(firstdate));
But since sdf.format is returning to a string,after formating 1 time i cant format it again to my desired Monthname day,Year format before displaying to user.
i mean if sdf.format(firstdate) returns "01/01/2000" string,i would like to convert this to January 1,2000 and then display it to user.
First get the String and convert to a Date:
String s = date.getText(); // e.g. "01/01/2000"
Date d = sdf.parse(s); // parse the String to get a Date object
Then you need a second SimpleDateFormat to convert the Date to the desired String:
SimpleDateFormat sdf2 = new SimpleDateFormat("MMMMM dd, yyyy");
s = sdf2.format(d); // "January 1, 2000"
This question already has answers here:
How to parse date string to Date? [duplicate]
(6 answers)
Java string to date conversion
(17 answers)
Closed 6 years ago.
What is the best way to transform like October-2016 into an Date Object?
When I try to use Date date = new SimpleDateFormat("MMMM-yyyy").parse("October-2016");
it throws an exception, because it cannot convert this type of date.
since the string October-2016 is containing information related to a language YOU NEED TO SPECIFY the Locale in the instance of the SimpleDateFormat
Date date = new SimpleDateFormat("MMMM-yyyy", Locale.US).parse("October-2016");
String st = "October-2016";
DateFormat format = new SimpleDateFormat("MMMM-yyyy");
Date date = format.parse(st);
System.out.println(date);
Both working same:
Date date = new SimpleDateFormat("MMMM-yyyy").parse("October-2016");
Date date1 = new SimpleDateFormat("MMMM-yyyy", Locale.US).parse("October-2016");
System.out.println(date);
System.out.println(date1);
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
I need to convert date string into a another specific format.
For example: I've a date which can come as YYYY-MM-DD or YYYY-MM-DDThh:mm:ss±0000 and I need to convert that to something "yyyy-mm-ddTHH:MM:SSZ".
I tried few things using Java 8 API but could not find a way out.
Try this (Java 8):
String input = "2016-10-14T14:19:40-04:00";
String output = ZonedDateTime.parse(input).toInstant().toString();
System.out.println(output); // prints 2016-10-14T18:19:40Z
This works because the primary format of ZonedDateTime used by parse() is ISO_ZONED_DATE_TIME, so it will parse input in the format you described.
The output format you requested happens to be the default format of Instant, as returned by its toString() method, i.e. the ISO_INSTANT format.
Converting between the two (ZonedDateTime to Instant using the toInstant() method) will perform the timezone adjustment necessary.
Try this:
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd"); // or "YYYY-MM-DDThh:mm:ss±0000"
String dateInString = "1993-11-27";
Date date = inputFormat.parse(dateInString);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ssZ");
System.out.println(outputFormat.format(date));
This question already has answers here:
Parsing ISO-8601 DateTime with offset with colon in Java
(4 answers)
Closed 6 years ago.
I just needed a sample code block or suggestion to convert the following date string to utc time and find difference with current time in java?
date string "2016-03-21T15:58:36-04:00"
Thanks in advance
You need to format the date string in following way:
String string = "2016-03-21T15:58:36-04:00";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
Date date = format.parse(string);
Then you just need to use Date APIs to find the time difference.
public static long getMillisFrom(String inStrDate) {
DateFormat ISO_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
Date inDate = ISO_DATE_FORMAT.parse(inStrDate);
Date curDate = new Date();
long diffMillis = curDate().getTime() - inDate.getTime();
return diffMillis
}
This question already has answers here:
Unix epoch time to Java Date object
(7 answers)
Closed 9 years ago.
I need to convert a String which is a epoch (Unix time) format to a Date class an after a String formatted (dd/MM/yyyy).
Thank you for your help !
Unix time is the number of seconds since 1 January 1970, so this should work
Date date = new Date(unixTime * 1000);
String str = new SimpleDateFormat("dd/MM/yyyy").format(date);
BTW SimpleDateFormat accepts millis as argument too, so it is possible to get the same result as
String str = new SimpleDateFormat("dd/MM/yyyy").format(unixTime * 1000);
Date date = new Date(time);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(date);
System.out.println(formatted);