Convert java.util.Date to String in yyyy-MM-dd format without creating a lot of objects
Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
I need to convert java.util.Date to String in yyyy-MM-dd format in a big amounts.
I have just moved to java 8 and want to know how to do it properly.
When using java 7 I was using
DateTimeFormatter DATE_FORMATTER = DateTimeFormat.forPattern(DATE_FORMAT_PATTERN)
and then
DATE_FORMATTER.print(value.getTime())
it helped me not to create a lots of redundant objects
So now when I moved to java 8 I want rewrite it properly but
LocalDate.fromDateFields(value).toString())
creates each time new LocalDate object and this gives a lot of work to GC
Are there any ways to solve my problem?
Performance and thread-safety are very important
java java-8 java-time
add a comment |Â
up vote
8
down vote
favorite
I need to convert java.util.Date to String in yyyy-MM-dd format in a big amounts.
I have just moved to java 8 and want to know how to do it properly.
When using java 7 I was using
DateTimeFormatter DATE_FORMATTER = DateTimeFormat.forPattern(DATE_FORMAT_PATTERN)
and then
DATE_FORMATTER.print(value.getTime())
it helped me not to create a lots of redundant objects
So now when I moved to java 8 I want rewrite it properly but
LocalDate.fromDateFields(value).toString())
creates each time new LocalDate object and this gives a lot of work to GC
Are there any ways to solve my problem?
Performance and thread-safety are very important
java java-8 java-time
1
Do you haveDate
orLocalDate
objects?
– Flown
1 hour ago
Hi, java.util.Date
– Oleksandr Riznyk
55 mins ago
There is no need converting toLocalDate
, just use theSimpleDateFormat
.
– Flown
53 mins ago
add a comment |Â
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I need to convert java.util.Date to String in yyyy-MM-dd format in a big amounts.
I have just moved to java 8 and want to know how to do it properly.
When using java 7 I was using
DateTimeFormatter DATE_FORMATTER = DateTimeFormat.forPattern(DATE_FORMAT_PATTERN)
and then
DATE_FORMATTER.print(value.getTime())
it helped me not to create a lots of redundant objects
So now when I moved to java 8 I want rewrite it properly but
LocalDate.fromDateFields(value).toString())
creates each time new LocalDate object and this gives a lot of work to GC
Are there any ways to solve my problem?
Performance and thread-safety are very important
java java-8 java-time
I need to convert java.util.Date to String in yyyy-MM-dd format in a big amounts.
I have just moved to java 8 and want to know how to do it properly.
When using java 7 I was using
DateTimeFormatter DATE_FORMATTER = DateTimeFormat.forPattern(DATE_FORMAT_PATTERN)
and then
DATE_FORMATTER.print(value.getTime())
it helped me not to create a lots of redundant objects
So now when I moved to java 8 I want rewrite it properly but
LocalDate.fromDateFields(value).toString())
creates each time new LocalDate object and this gives a lot of work to GC
Are there any ways to solve my problem?
Performance and thread-safety are very important
java java-8 java-time
java java-8 java-time
edited 38 mins ago
asked 1 hour ago
Oleksandr Riznyk
21018
21018
1
Do you haveDate
orLocalDate
objects?
– Flown
1 hour ago
Hi, java.util.Date
– Oleksandr Riznyk
55 mins ago
There is no need converting toLocalDate
, just use theSimpleDateFormat
.
– Flown
53 mins ago
add a comment |Â
1
Do you haveDate
orLocalDate
objects?
– Flown
1 hour ago
Hi, java.util.Date
– Oleksandr Riznyk
55 mins ago
There is no need converting toLocalDate
, just use theSimpleDateFormat
.
– Flown
53 mins ago
1
1
Do you have
Date
or LocalDate
objects?– Flown
1 hour ago
Do you have
Date
or LocalDate
objects?– Flown
1 hour ago
Hi, java.util.Date
– Oleksandr Riznyk
55 mins ago
Hi, java.util.Date
– Oleksandr Riznyk
55 mins ago
There is no need converting to
LocalDate
, just use the SimpleDateFormat
.– Flown
53 mins ago
There is no need converting to
LocalDate
, just use the SimpleDateFormat
.– Flown
53 mins ago
add a comment |Â
6 Answers
6
active
oldest
votes
up vote
3
down vote
The following only has an overhead for the conversion of the old Date to the new LocalDate.
Date date = new Date();
LocalDate ldate = LocalDate.from(date.toInstant());
String s = DateTimeFormatter.ISO_DATE.format(ldate); // uuuu-MM-dd
It is true however that DateTimeFormatters are thread-safe and hence will have one instantiation more per call.
Good idea I didn't notice that I can usedate.toInstant()
already with the static methodLocalDate.from()
, this reduces the part of specifing a time zone. +1
– 0x1C1B
41 mins ago
1
This snippet throws an exception on 1.8.0_181 and 11
– Flown
36 mins ago
@Flown You're right also on JDK10 what's the problem?
– 0x1C1B
33 mins ago
1
Should beLocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
– Flown
31 mins ago
@Flown IsLocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
faster thandate.toInstant().atZone(ZoneOffset.UTC).toLocalDate();
?
– 0x1C1B
23 mins ago
 |Â
show 2 more comments
up vote
1
down vote
I don't have exact numbers from point of view of the performance but I would use the Java 8 Time API to solve this issue. In your special case I would use the following statement:
LocalDate.now().format(DateTimeFormatter.ISO_DATE);
EDIT:
For this solution is a conversion from java.util.Date
to java.time.LocalDate
required!
new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
add a comment |Â
up vote
1
down vote
Use SimpleDateFormat to format Date.
watch out, SDF is NOT THREAD-SAFE, it might not be important but keep that in mind.
For Example:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println((sdf.format(new Date())).toString());
LINK with more information.
The method parse(String) in the type DateFormat is not applicable for the arguments (Date)
– Selaron
47 mins ago
yeah, i meant format :) nice catch
– Emil Hotkowski
41 mins ago
add a comment |Â
up vote
0
down vote
I would suggest you try out Joda Time for DateTime-related operations.
Joda Time Maven
thanks but my solution from Java 7 is already uses Joda Time. I would like do know if Java 8 provides any better solutions?
– Oleksandr Riznyk
1 hour ago
add a comment |Â
up vote
0
down vote
Without creating lots of objects, meaning you want the performance version?
public static String getIsoDate(java.util.Date time)
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTime(time);
StringBuilder sb = new StringBuilder();
int year = cal.get(java.util.Calendar.YEAR);
int month = cal.get(java.util.Calendar.MONTH);
int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
sb.append(year);
sb.append('-');
if (month < 10)
sb.append('0');
sb.append(month);
sb.append('-');
if (day < 10)
sb.append('0');
sb.append(day);
return sb.toString();
This version is thread-safe and avoids most hidden object creations, and unless you have years under 1000 or over 9999, it will print just fine.
This isn't offensive it's actually interest, are you sure that this code is faster than converting it toLocalDate
and using the Java 8 Time API? I mean, I'm definetly not a performance freak, but it seems that this is a lot of code with memory allocation... Seems to be interesting...
– 0x1C1B
25 mins ago
add a comment |Â
up vote
-1
down vote
Use SimpleDateFormat
to format Date in your expected format.
For Example:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println((sdf.parse("2019-08-31")).toString());
Hi, i need to convert java.util.Date object to String, but your code is used for parsing Date from String which I do not have at this moment
– Oleksandr Riznyk
55 mins ago
how do you want to do it then?
– Busy Bee
49 mins ago
1
I don't understand your question but in your solution you parsing a date from string but my target is to parse string it is the question
– Oleksandr Riznyk
44 mins ago
add a comment |Â
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
The following only has an overhead for the conversion of the old Date to the new LocalDate.
Date date = new Date();
LocalDate ldate = LocalDate.from(date.toInstant());
String s = DateTimeFormatter.ISO_DATE.format(ldate); // uuuu-MM-dd
It is true however that DateTimeFormatters are thread-safe and hence will have one instantiation more per call.
Good idea I didn't notice that I can usedate.toInstant()
already with the static methodLocalDate.from()
, this reduces the part of specifing a time zone. +1
– 0x1C1B
41 mins ago
1
This snippet throws an exception on 1.8.0_181 and 11
– Flown
36 mins ago
@Flown You're right also on JDK10 what's the problem?
– 0x1C1B
33 mins ago
1
Should beLocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
– Flown
31 mins ago
@Flown IsLocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
faster thandate.toInstant().atZone(ZoneOffset.UTC).toLocalDate();
?
– 0x1C1B
23 mins ago
 |Â
show 2 more comments
up vote
3
down vote
The following only has an overhead for the conversion of the old Date to the new LocalDate.
Date date = new Date();
LocalDate ldate = LocalDate.from(date.toInstant());
String s = DateTimeFormatter.ISO_DATE.format(ldate); // uuuu-MM-dd
It is true however that DateTimeFormatters are thread-safe and hence will have one instantiation more per call.
Good idea I didn't notice that I can usedate.toInstant()
already with the static methodLocalDate.from()
, this reduces the part of specifing a time zone. +1
– 0x1C1B
41 mins ago
1
This snippet throws an exception on 1.8.0_181 and 11
– Flown
36 mins ago
@Flown You're right also on JDK10 what's the problem?
– 0x1C1B
33 mins ago
1
Should beLocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
– Flown
31 mins ago
@Flown IsLocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
faster thandate.toInstant().atZone(ZoneOffset.UTC).toLocalDate();
?
– 0x1C1B
23 mins ago
 |Â
show 2 more comments
up vote
3
down vote
up vote
3
down vote
The following only has an overhead for the conversion of the old Date to the new LocalDate.
Date date = new Date();
LocalDate ldate = LocalDate.from(date.toInstant());
String s = DateTimeFormatter.ISO_DATE.format(ldate); // uuuu-MM-dd
It is true however that DateTimeFormatters are thread-safe and hence will have one instantiation more per call.
The following only has an overhead for the conversion of the old Date to the new LocalDate.
Date date = new Date();
LocalDate ldate = LocalDate.from(date.toInstant());
String s = DateTimeFormatter.ISO_DATE.format(ldate); // uuuu-MM-dd
It is true however that DateTimeFormatters are thread-safe and hence will have one instantiation more per call.
answered 51 mins ago


Joop Eggen
74.4k754100
74.4k754100
Good idea I didn't notice that I can usedate.toInstant()
already with the static methodLocalDate.from()
, this reduces the part of specifing a time zone. +1
– 0x1C1B
41 mins ago
1
This snippet throws an exception on 1.8.0_181 and 11
– Flown
36 mins ago
@Flown You're right also on JDK10 what's the problem?
– 0x1C1B
33 mins ago
1
Should beLocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
– Flown
31 mins ago
@Flown IsLocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
faster thandate.toInstant().atZone(ZoneOffset.UTC).toLocalDate();
?
– 0x1C1B
23 mins ago
 |Â
show 2 more comments
Good idea I didn't notice that I can usedate.toInstant()
already with the static methodLocalDate.from()
, this reduces the part of specifing a time zone. +1
– 0x1C1B
41 mins ago
1
This snippet throws an exception on 1.8.0_181 and 11
– Flown
36 mins ago
@Flown You're right also on JDK10 what's the problem?
– 0x1C1B
33 mins ago
1
Should beLocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
– Flown
31 mins ago
@Flown IsLocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
faster thandate.toInstant().atZone(ZoneOffset.UTC).toLocalDate();
?
– 0x1C1B
23 mins ago
Good idea I didn't notice that I can use
date.toInstant()
already with the static method LocalDate.from()
, this reduces the part of specifing a time zone. +1– 0x1C1B
41 mins ago
Good idea I didn't notice that I can use
date.toInstant()
already with the static method LocalDate.from()
, this reduces the part of specifing a time zone. +1– 0x1C1B
41 mins ago
1
1
This snippet throws an exception on 1.8.0_181 and 11
– Flown
36 mins ago
This snippet throws an exception on 1.8.0_181 and 11
– Flown
36 mins ago
@Flown You're right also on JDK10 what's the problem?
– 0x1C1B
33 mins ago
@Flown You're right also on JDK10 what's the problem?
– 0x1C1B
33 mins ago
1
1
Should be
LocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
– Flown
31 mins ago
Should be
LocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
– Flown
31 mins ago
@Flown Is
LocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
faster than date.toInstant().atZone(ZoneOffset.UTC).toLocalDate();
?– 0x1C1B
23 mins ago
@Flown Is
LocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
faster than date.toInstant().atZone(ZoneOffset.UTC).toLocalDate();
?– 0x1C1B
23 mins ago
 |Â
show 2 more comments
up vote
1
down vote
I don't have exact numbers from point of view of the performance but I would use the Java 8 Time API to solve this issue. In your special case I would use the following statement:
LocalDate.now().format(DateTimeFormatter.ISO_DATE);
EDIT:
For this solution is a conversion from java.util.Date
to java.time.LocalDate
required!
new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
add a comment |Â
up vote
1
down vote
I don't have exact numbers from point of view of the performance but I would use the Java 8 Time API to solve this issue. In your special case I would use the following statement:
LocalDate.now().format(DateTimeFormatter.ISO_DATE);
EDIT:
For this solution is a conversion from java.util.Date
to java.time.LocalDate
required!
new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I don't have exact numbers from point of view of the performance but I would use the Java 8 Time API to solve this issue. In your special case I would use the following statement:
LocalDate.now().format(DateTimeFormatter.ISO_DATE);
EDIT:
For this solution is a conversion from java.util.Date
to java.time.LocalDate
required!
new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
I don't have exact numbers from point of view of the performance but I would use the Java 8 Time API to solve this issue. In your special case I would use the following statement:
LocalDate.now().format(DateTimeFormatter.ISO_DATE);
EDIT:
For this solution is a conversion from java.util.Date
to java.time.LocalDate
required!
new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
edited 48 mins ago
answered 53 mins ago
0x1C1B
47012
47012
add a comment |Â
add a comment |Â
up vote
1
down vote
Use SimpleDateFormat to format Date.
watch out, SDF is NOT THREAD-SAFE, it might not be important but keep that in mind.
For Example:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println((sdf.format(new Date())).toString());
LINK with more information.
The method parse(String) in the type DateFormat is not applicable for the arguments (Date)
– Selaron
47 mins ago
yeah, i meant format :) nice catch
– Emil Hotkowski
41 mins ago
add a comment |Â
up vote
1
down vote
Use SimpleDateFormat to format Date.
watch out, SDF is NOT THREAD-SAFE, it might not be important but keep that in mind.
For Example:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println((sdf.format(new Date())).toString());
LINK with more information.
The method parse(String) in the type DateFormat is not applicable for the arguments (Date)
– Selaron
47 mins ago
yeah, i meant format :) nice catch
– Emil Hotkowski
41 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Use SimpleDateFormat to format Date.
watch out, SDF is NOT THREAD-SAFE, it might not be important but keep that in mind.
For Example:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println((sdf.format(new Date())).toString());
LINK with more information.
Use SimpleDateFormat to format Date.
watch out, SDF is NOT THREAD-SAFE, it might not be important but keep that in mind.
For Example:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println((sdf.format(new Date())).toString());
LINK with more information.
edited 42 mins ago
answered 52 mins ago


Emil Hotkowski
758312
758312
The method parse(String) in the type DateFormat is not applicable for the arguments (Date)
– Selaron
47 mins ago
yeah, i meant format :) nice catch
– Emil Hotkowski
41 mins ago
add a comment |Â
The method parse(String) in the type DateFormat is not applicable for the arguments (Date)
– Selaron
47 mins ago
yeah, i meant format :) nice catch
– Emil Hotkowski
41 mins ago
The method parse(String) in the type DateFormat is not applicable for the arguments (Date)
– Selaron
47 mins ago
The method parse(String) in the type DateFormat is not applicable for the arguments (Date)
– Selaron
47 mins ago
yeah, i meant format :) nice catch
– Emil Hotkowski
41 mins ago
yeah, i meant format :) nice catch
– Emil Hotkowski
41 mins ago
add a comment |Â
up vote
0
down vote
I would suggest you try out Joda Time for DateTime-related operations.
Joda Time Maven
thanks but my solution from Java 7 is already uses Joda Time. I would like do know if Java 8 provides any better solutions?
– Oleksandr Riznyk
1 hour ago
add a comment |Â
up vote
0
down vote
I would suggest you try out Joda Time for DateTime-related operations.
Joda Time Maven
thanks but my solution from Java 7 is already uses Joda Time. I would like do know if Java 8 provides any better solutions?
– Oleksandr Riznyk
1 hour ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I would suggest you try out Joda Time for DateTime-related operations.
Joda Time Maven
I would suggest you try out Joda Time for DateTime-related operations.
Joda Time Maven
answered 1 hour ago


Nagaraja R
43
43
thanks but my solution from Java 7 is already uses Joda Time. I would like do know if Java 8 provides any better solutions?
– Oleksandr Riznyk
1 hour ago
add a comment |Â
thanks but my solution from Java 7 is already uses Joda Time. I would like do know if Java 8 provides any better solutions?
– Oleksandr Riznyk
1 hour ago
thanks but my solution from Java 7 is already uses Joda Time. I would like do know if Java 8 provides any better solutions?
– Oleksandr Riznyk
1 hour ago
thanks but my solution from Java 7 is already uses Joda Time. I would like do know if Java 8 provides any better solutions?
– Oleksandr Riznyk
1 hour ago
add a comment |Â
up vote
0
down vote
Without creating lots of objects, meaning you want the performance version?
public static String getIsoDate(java.util.Date time)
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTime(time);
StringBuilder sb = new StringBuilder();
int year = cal.get(java.util.Calendar.YEAR);
int month = cal.get(java.util.Calendar.MONTH);
int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
sb.append(year);
sb.append('-');
if (month < 10)
sb.append('0');
sb.append(month);
sb.append('-');
if (day < 10)
sb.append('0');
sb.append(day);
return sb.toString();
This version is thread-safe and avoids most hidden object creations, and unless you have years under 1000 or over 9999, it will print just fine.
This isn't offensive it's actually interest, are you sure that this code is faster than converting it toLocalDate
and using the Java 8 Time API? I mean, I'm definetly not a performance freak, but it seems that this is a lot of code with memory allocation... Seems to be interesting...
– 0x1C1B
25 mins ago
add a comment |Â
up vote
0
down vote
Without creating lots of objects, meaning you want the performance version?
public static String getIsoDate(java.util.Date time)
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTime(time);
StringBuilder sb = new StringBuilder();
int year = cal.get(java.util.Calendar.YEAR);
int month = cal.get(java.util.Calendar.MONTH);
int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
sb.append(year);
sb.append('-');
if (month < 10)
sb.append('0');
sb.append(month);
sb.append('-');
if (day < 10)
sb.append('0');
sb.append(day);
return sb.toString();
This version is thread-safe and avoids most hidden object creations, and unless you have years under 1000 or over 9999, it will print just fine.
This isn't offensive it's actually interest, are you sure that this code is faster than converting it toLocalDate
and using the Java 8 Time API? I mean, I'm definetly not a performance freak, but it seems that this is a lot of code with memory allocation... Seems to be interesting...
– 0x1C1B
25 mins ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Without creating lots of objects, meaning you want the performance version?
public static String getIsoDate(java.util.Date time)
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTime(time);
StringBuilder sb = new StringBuilder();
int year = cal.get(java.util.Calendar.YEAR);
int month = cal.get(java.util.Calendar.MONTH);
int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
sb.append(year);
sb.append('-');
if (month < 10)
sb.append('0');
sb.append(month);
sb.append('-');
if (day < 10)
sb.append('0');
sb.append(day);
return sb.toString();
This version is thread-safe and avoids most hidden object creations, and unless you have years under 1000 or over 9999, it will print just fine.
Without creating lots of objects, meaning you want the performance version?
public static String getIsoDate(java.util.Date time)
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTime(time);
StringBuilder sb = new StringBuilder();
int year = cal.get(java.util.Calendar.YEAR);
int month = cal.get(java.util.Calendar.MONTH);
int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
sb.append(year);
sb.append('-');
if (month < 10)
sb.append('0');
sb.append(month);
sb.append('-');
if (day < 10)
sb.append('0');
sb.append(day);
return sb.toString();
This version is thread-safe and avoids most hidden object creations, and unless you have years under 1000 or over 9999, it will print just fine.
answered 32 mins ago
coladict
1,601517
1,601517
This isn't offensive it's actually interest, are you sure that this code is faster than converting it toLocalDate
and using the Java 8 Time API? I mean, I'm definetly not a performance freak, but it seems that this is a lot of code with memory allocation... Seems to be interesting...
– 0x1C1B
25 mins ago
add a comment |Â
This isn't offensive it's actually interest, are you sure that this code is faster than converting it toLocalDate
and using the Java 8 Time API? I mean, I'm definetly not a performance freak, but it seems that this is a lot of code with memory allocation... Seems to be interesting...
– 0x1C1B
25 mins ago
This isn't offensive it's actually interest, are you sure that this code is faster than converting it to
LocalDate
and using the Java 8 Time API? I mean, I'm definetly not a performance freak, but it seems that this is a lot of code with memory allocation... Seems to be interesting...– 0x1C1B
25 mins ago
This isn't offensive it's actually interest, are you sure that this code is faster than converting it to
LocalDate
and using the Java 8 Time API? I mean, I'm definetly not a performance freak, but it seems that this is a lot of code with memory allocation... Seems to be interesting...– 0x1C1B
25 mins ago
add a comment |Â
up vote
-1
down vote
Use SimpleDateFormat
to format Date in your expected format.
For Example:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println((sdf.parse("2019-08-31")).toString());
Hi, i need to convert java.util.Date object to String, but your code is used for parsing Date from String which I do not have at this moment
– Oleksandr Riznyk
55 mins ago
how do you want to do it then?
– Busy Bee
49 mins ago
1
I don't understand your question but in your solution you parsing a date from string but my target is to parse string it is the question
– Oleksandr Riznyk
44 mins ago
add a comment |Â
up vote
-1
down vote
Use SimpleDateFormat
to format Date in your expected format.
For Example:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println((sdf.parse("2019-08-31")).toString());
Hi, i need to convert java.util.Date object to String, but your code is used for parsing Date from String which I do not have at this moment
– Oleksandr Riznyk
55 mins ago
how do you want to do it then?
– Busy Bee
49 mins ago
1
I don't understand your question but in your solution you parsing a date from string but my target is to parse string it is the question
– Oleksandr Riznyk
44 mins ago
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
Use SimpleDateFormat
to format Date in your expected format.
For Example:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println((sdf.parse("2019-08-31")).toString());
Use SimpleDateFormat
to format Date in your expected format.
For Example:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println((sdf.parse("2019-08-31")).toString());
answered 58 mins ago


Busy Bee
7251618
7251618
Hi, i need to convert java.util.Date object to String, but your code is used for parsing Date from String which I do not have at this moment
– Oleksandr Riznyk
55 mins ago
how do you want to do it then?
– Busy Bee
49 mins ago
1
I don't understand your question but in your solution you parsing a date from string but my target is to parse string it is the question
– Oleksandr Riznyk
44 mins ago
add a comment |Â
Hi, i need to convert java.util.Date object to String, but your code is used for parsing Date from String which I do not have at this moment
– Oleksandr Riznyk
55 mins ago
how do you want to do it then?
– Busy Bee
49 mins ago
1
I don't understand your question but in your solution you parsing a date from string but my target is to parse string it is the question
– Oleksandr Riznyk
44 mins ago
Hi, i need to convert java.util.Date object to String, but your code is used for parsing Date from String which I do not have at this moment
– Oleksandr Riznyk
55 mins ago
Hi, i need to convert java.util.Date object to String, but your code is used for parsing Date from String which I do not have at this moment
– Oleksandr Riznyk
55 mins ago
how do you want to do it then?
– Busy Bee
49 mins ago
how do you want to do it then?
– Busy Bee
49 mins ago
1
1
I don't understand your question but in your solution you parsing a date from string but my target is to parse string it is the question
– Oleksandr Riznyk
44 mins ago
I don't understand your question but in your solution you parsing a date from string but my target is to parse string it is the question
– Oleksandr Riznyk
44 mins ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52870774%2fconvert-java-util-date-to-string-in-yyyy-mm-dd-format-without-creating-a-lot-of%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Do you have
Date
orLocalDate
objects?– Flown
1 hour ago
Hi, java.util.Date
– Oleksandr Riznyk
55 mins ago
There is no need converting to
LocalDate
, just use theSimpleDateFormat
.– Flown
53 mins ago