Convert Java.Util.Date to String

Convert java.util.Date to String

Convert a Date to a String using DateFormat#format method:

String pattern = "MM/dd/yyyy HH:mm:ss";

// Create an instance of SimpleDateFormat used for formatting
// the string representation of date according to the chosen pattern
DateFormat df = new SimpleDateFormat(pattern);

// Get the today date using Calendar object.
Date today = Calendar.getInstance().getTime();
// Using DateFormat format method we can create a string
// representation of a date with the defined format.
String todayAsString = df.format(today);

// Print the result!
System.out.println("Today is: " + todayAsString);

From http://www.kodejava.org/examples/86.html

Convert java.util.Date to String in yyyy-MM-dd format without creating a lot of objects

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().atZone(ZoneOffset.UTC));
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.

P.S.

I added .atZone(ZoneOffset.UTC) because of a reported exception, and @Flown's solution: specifying the zone. As Date is not necessarily used for UTC dates, one might use another one.

How to convert java.util.Date to String

you can try this code:

public String getBirthDay(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
return sdf.format(birthDay);
}

How can I change java.util.Date to ISO String using ThreeTenABP

A ThreetenABP-solution can look like this:

java.util.Date d = ...;
org.threeten.bp.Instant instant = org.threeten.bp.DateTimeUtils.toInstant(d);
String iso = instant.toString();

If you wish more control over formatting then you can convert the instant to a ZonedDateTime (or better to an OffsetDateTime) and use a dedicated DateTimeFormatter.

convert java.util.Date to java.util.Date with different formating in JAVA

tl;dr

Convert from legacy class to modern class. Adjust from UTC to a time zone. Generate text in standard ISO 8601. We omit the context of time zone or offset in our output because you so requested, against my recommendation.

myJavaUtilDate
.toInstant()
.atZone( ZoneId.of( "Asia/Kolkata" ) )
.truncatedTo( ChronoUnit.MINUTES )
.format( DateTimeFormatter.ISO_LOCAL_DATE_TIME )

I expect using UTC and including the offset would be wiser.

myJavaUtilDate
.toInstant()
.toString()

Details

Date-time objects do not have a format, only text has a format.

Use java.time classes, never java.util.Date.

Convert your legacy Date object to its modern replacement, java.time.Instant.

Instant instant = myJUDate.toInstant() ;

Adjust from UTC to your desired time zone.

ZoneId z = ZoneId.of( "Asia/Kolkata" ) ; 
ZonedDateTime zdt = instant.atZone( z ) ;

Apparently you do not care about the the second of minute. So let’s truncate that to zero seconds.

ZonedDateTime zdt = zdt.truncatedTo( ChronoUnit.MINUTES ) ;

Generate text in your desired format. Java comes bundled with a formatter already defined for your format.

String output = zdt.format( DateTimeFormatter.ISO_LOCAL_DATE_TIME ) ;

I showed that format because asked. But I do not recommend it. That format fails to indicate a time zone or offset-from-UTC. So if it says noon, the reader does not know if that means noon in Tokyo Japan , noon in Toulouse France , or noon in Toledo Ohio Us — three very different moments, several hours apart.

When communicating a moment, a specific point on the timeline, textually it is usually best to do so in UTC. And use ISO 8601 standard formats. Commonly a Z is placed on the end to indicate UTC, an offset of zero hours-minutes-seconds.

Instant instant = zdt.toInstant() ;
String output = instant.toString() ;

How to convert Java.Util.Date to System.DateTime

java.util.Date has a getTime() method, which returns the date as a millisecond value. The value is the number of milliseconds since Jan. 1, 1970, midnight GMT.

With that knowledge, you can construct a System.DateTime, that matches this value like so:

public DateTime FromUnixTime(long unixTimeMillis)
{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return epoch.AddMilliseconds(unixTimeMillis);
}

(method taken from this answer)



Related Topics



Leave a reply



Submit