Skip to content

Instantly share code, notes, and snippets.

@odrotbohm
Created November 26, 2016 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odrotbohm/d1a0335a196c6c3146b420c9351533fa to your computer and use it in GitHub Desktop.
Save odrotbohm/d1a0335a196c6c3146b420c9351533fa to your computer and use it in GitHub Desktop.
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.example.myapi;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;
import org.junit.Test;
public class DateFormattingTests {
private static final int ITERATIONS = 5000000;
@Test
public void testname() {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
long startTime = System.currentTimeMillis();
for (int i = 0; i < ITERATIONS; i++) {
formatter.format(now);
}
long duration = (System.currentTimeMillis() - startTime);
System.out.println("Converting LocalDateTime took " + duration + "ms");
}
@Test
public void testname2() {
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); // Quoted "Z" to indicate UTC, no timezone offset
df.setTimeZone(tz);
Date now = new Date();
long startTime = System.currentTimeMillis();
for (int i = 0; i < ITERATIONS; i++) {
df.format(now);
}
long duration = (System.currentTimeMillis() - startTime);
System.out.println("Converting Date took " + duration + "ms");
}
}
Converting Date took 3944ms
Converting LocalDateTime took 18020ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment