Skip to content

Commit 3f131ea

Browse files
committed
LogView: Date column should be more fine grained
- and show yyyy-MM-dd HH:mm:ss.SSS instead of just minute precision - this is useful where you want to see the exact time of log events in cases where seconds or even milliseconds matter add and use getFormattedDate() reuse LogEntry DateTimeFormatters fix potential NPE in case somebody calls getFormattedDate before setDate() was called
1 parent 70a35a9 commit 3f131ea

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

bundles/org.eclipse.ui.views.log/src/org/eclipse/ui/internal/views/log/LogEntry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public class LogEntry extends AbstractEntry {
3131

3232
public static final String SPACE = " "; //$NON-NLS-1$
3333
public static final String F_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; //$NON-NLS-1$
34-
private static final DateTimeFormatter GREGORIAN_SDF = DateTimeFormatter.ofPattern(F_DATE_FORMAT, Locale.ENGLISH)
34+
static final DateTimeFormatter GREGORIAN_SDF = DateTimeFormatter.ofPattern(F_DATE_FORMAT, Locale.ENGLISH)
3535
.withZone(ZoneId.systemDefault());
36-
private static final DateTimeFormatter LOCAL_SDF = DateTimeFormatter.ofPattern(F_DATE_FORMAT)
36+
static final DateTimeFormatter LOCAL_SDF = DateTimeFormatter.ofPattern(F_DATE_FORMAT)
3737
.withZone(ZoneId.systemDefault());
3838

3939
private String pluginId;

bundles/org.eclipse.ui.views.log/src/org/eclipse/ui/internal/views/log/LogSession.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
*******************************************************************************/
1616
package org.eclipse.ui.internal.views.log;
1717

18+
import static org.eclipse.ui.internal.views.log.LogEntry.GREGORIAN_SDF;
19+
import static org.eclipse.ui.internal.views.log.LogEntry.LOCAL_SDF;
20+
1821
import java.io.PrintWriter;
19-
import java.text.ParseException;
20-
import java.text.SimpleDateFormat;
22+
import java.time.Instant;
2123
import java.util.Date;
2224

2325
/**
@@ -31,8 +33,10 @@ public class LogSession extends Group {
3133
* @since 3.5
3234
*/
3335
public static final String SESSION = "!SESSION"; //$NON-NLS-1$
36+
3437
private String sessionData;
3538
private Date date;
39+
private String fDateString;
3640

3741
public LogSession() {
3842
super(Messages.LogViewLabelProvider_Session);
@@ -42,11 +46,30 @@ public Date getDate() {
4246
return date;
4347
}
4448

49+
/**
50+
* Returns a pretty-print formatting for the date for this entry
51+
*
52+
* @return the formatted date for this entry
53+
*/
54+
public String getFormattedDate() {
55+
if (fDateString == null) {
56+
Date tmpdate = getDate();
57+
if (tmpdate != null) {
58+
fDateString = LOCAL_SDF.format(tmpdate.toInstant());
59+
}
60+
}
61+
return fDateString;
62+
}
63+
4564
public void setDate(String dateString) {
46-
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); //$NON-NLS-1$
4765
try {
48-
date = formatter.parse(dateString);
49-
} catch (ParseException e) { // do nothing
66+
Date parsed = Date.from(Instant.from(GREGORIAN_SDF.parse(dateString)));
67+
if (parsed != null) {
68+
this.date = parsed;
69+
fDateString = LOCAL_SDF.format(parsed.toInstant());
70+
}
71+
} catch (Exception e) {
72+
// do nothing
5073
}
5174
}
5275

bundles/org.eclipse.ui.views.log/src/org/eclipse/ui/internal/views/log/LogViewLabelProvider.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*******************************************************************************/
1616
package org.eclipse.ui.internal.views.log;
1717

18-
import java.text.DateFormat;
1918
import java.util.ArrayList;
2019
import org.eclipse.core.runtime.IStatus;
2120
import org.eclipse.jface.resource.JFaceResources;
@@ -34,8 +33,6 @@ public class LogViewLabelProvider extends LabelProvider implements ITableLabelPr
3433
private Image errorWithStackImage;
3534
private Image hierarchicalImage;
3635
ArrayList<Object> consumers = new ArrayList<>();
37-
private DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
38-
3936
private LogView logView;
4037

4138
public LogViewLabelProvider(LogView logView) {
@@ -81,7 +78,7 @@ public String getColumnText(Object element, int columnIndex) {
8178
if (session.getDate() == null)
8279
return ""; //$NON-NLS-1$
8380

84-
return dateFormat.format(session.getDate());
81+
return session.getFormattedDate();
8582
}
8683

8784
if ((element instanceof Group) && (columnIndex == 0)) {
@@ -106,7 +103,7 @@ public String getColumnText(Object element, int columnIndex) {
106103
if (entry.getPluginId() != null)
107104
return entry.getPluginId();
108105
case 2 :
109-
return dateFormat.format(entry.getDate());
106+
return entry.getFormattedDate();
110107
}
111108
}
112109

0 commit comments

Comments
 (0)