Reputation: 1257
20160222082641Z
This kind of timestamp is logged using some LDAP functionality. I need to duplicate it in a Java program.
My team members are unable to tell me how to do this or really give me any kind of useful help. Would anyone be familiar with how to generate this in Java? Thanks
Upvotes: 0
Views: 46
Reputation: 897
Use SimpleDateformat:
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
String ldapDate = format.format(Calendar.getInstance().getTime()) + "Z";
System.out.println(ldapDate);
Upvotes: 1
Reputation: 207
Using Java 8, the following imports are needed:
import java.time.LocalDateTime;
import static java.time.ZoneOffset.UTC;
Using Java 8, the following code should work:
LocalDateTime now = LocalDateTime.now(UTC);
String timeString = now.toString();
String strOut = timeString.substring(0,4)
+ timeString.substring(5,7) + timeString.substring(8,10)
+ timeString.substring(11,13) + timeString.substring(14,16)
+ timeString.substring(17,19) + "Z";
System.out.println(now);
System.out.println(strOut);
Upvotes: 0