This question already has answers here:
How can I change the date format in Java? [duplicate]
(10 answers)
Closed 5 years ago.
I have a date in String format: 2017-05-10.
I need to transform this string in this 10/05/2017. How i can do?
Which library i can use?
I have tried this but not works
DateFormat df = new SimpleDateFormat("dd-MM-yyyy",Locale.ITALIAN);
Calendar cal = Calendar.getInstance();
cal.setTime(df.parse(data));
Can also be done using the java.time API:
String date = "2017-05-10";
LocalDate ld = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
String formattedDate = DateTimeFormatter.ofPattern("dd/MM/yyyy").format(ld);
You can try following code:
DateFormat dfto = new SimpleDateFormat("dd/MM/yyyy");
DateFormat dffrom = new SimpleDateFormat("yyyy-MM-dd");
Date today = dffrom.parse("2017-05-10");
String s = dfto.format(today);
I would simply run an internal Java function that comes built in with the String object, described below:
replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of
oldChar in this string with newChar.
String myDate = df.parseDate(data).toString();
// if that's the right string you mention
// then
cal.setTime(myDate.replace('-', '/'));
They may have to be escaped but since it's char data type my initial thought is no.
Related
This question already has answers here:
java.time.format.DateTimeParseException, Date could not be parsed
(1 answer)
Create LocalDate Object from Integers
(3 answers)
Invalid localdate when it created
(1 answer)
Can't parse String to LocalDate (Java 8)
(2 answers)
Difference between year-of-era and week-based-year?
(7 answers)
Closed 3 months ago.
I want to convert date from String to LocalDate object: Starting date as String: "12/11/2022" I want to convert this String to LocalDate. I want to get LocalDate object and next use this LocalData inside if-else conditions to convert this object to String and return String. All conditions return first LocalData and next String as result: if I use this code with: date = LocalDate.parse(default)};
public String getStringDate(String timeProjection){
if (timeProjection.equals("1") {
date = LocalDate.now();
}
....
else {
String default = "12/11/2022";
date = LocalDate.parse(default);
}
command
return date.format(DataTimeFormatter.ofPattern("dd/MM/YYYY"));
I get issue that is impossible this parse, but when I use:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/YYYY");
String date = "16/08/2016";
LocalDate localDate = LocalDate.parse(date, formatter);
This convertion works good. What do you think is possible create solution when I can't use twice this formatter: DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/YYYY") - first in else body, second in result command? Thanks for your help.
This question already has answers here:
How to convert Timestamp into Date format in java
(4 answers)
Closed 2 years ago.
I have a string I recieve from service is a date, this date have this format "/Date(1607490140063-0600)/" is a string, now I need convert from a string to a date format on java, I try differents ways but no work
String endTime = getPunchTime();
SimpleDateFormat timeFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss Z");
Date d2 = timeFormat.parse(endTime);
Write a helper method for parsing the value, and use regex to parse the text, e.g. like this:
static OffsetDateTime parseJsonDate(String s) {
Pattern p = Pattern.compile("/Date\\((-?\\d+)([+-]\\d{4})\\)/");
Matcher m = p.matcher(s);
if (! m.matches())
throw new DateTimeParseException("Not a valid JSON Date string: " + s, s, 0);
return Instant.ofEpochMilli(Long.parseLong(m.group(1))).atOffset(ZoneOffset.of(m.group(2)));
}
Test
System.out.println(parseJsonDate("/Date(1607490140063-0600)/"));
Output
2020-12-08T23:02:20.063-06:00
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:
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
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?