HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

Format current date

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
formatcurrentdate

Problem

I was refactoring some code and found this:

DateTime CurrentDateTime = System.now();
Datetime ESTDate = Datetime.newInstance(CurrentDateTime.year(),CurrentDateTime.month(),CurrentDateTime.day(),CurrentDateTime.hour(),CurrentDateTime.minute(),CurrentDateTime.second());
String myDateFormat = ESTDate.format('yyyy-MM-dd hh:mm:ss');
String myDate = myDateFormat.replace(' ', 'T');
object1.birthDate = myDate;


And I changed it to

object1.birthDate = datetime.now().format('yyyy-MM-dd\'T\'hh:mm:ss');


CurrentDateTime isn't used anywhere just in those four lines.
How a programmer can even do that? Or WHY?

Solution

The programmer probably didn't understand how quoting works in DateTime.format() - just couldn't get the 'T' to appear in the string, so bailed, put the space there and replaced it. Creating ESTDate from CurrentDateTime is particularly weird, though!

Context

StackExchange Code Review Q#21595, answer score: 5

Revisions (0)

No revisions yet.