Skip to content

Commit

Permalink
Use localized medium date format instead of ISO date format
Browse files Browse the repository at this point in the history
Issue: #441
  • Loading branch information
buchen committed Mar 25, 2016
1 parent a4d7b29 commit dd4dc31
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 28 deletions.
@@ -1,31 +1,22 @@
package name.abuchen.portfolio.ui.util.viewers;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

import name.abuchen.portfolio.ui.Messages;
import java.time.format.FormatStyle;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

import com.ibm.icu.text.MessageFormat;

import name.abuchen.portfolio.ui.Messages;

public class DateEditingSupport extends PropertyEditingSupport
{
public static class DateCharacterVerifyListener implements VerifyListener
{
private String allowedChars = "-0123456789"; //$NON-NLS-1$

public void verifyText(VerifyEvent e)
{
for (int ii = 0; e.doit && ii < e.text.length(); ii++)
e.doit = allowedChars.indexOf(e.text.charAt(0)) >= 0;
}
}
private static final DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);

public DateEditingSupport(Class<?> subjectType, String attributeName)
{
Expand All @@ -39,15 +30,15 @@ public DateEditingSupport(Class<?> subjectType, String attributeName)
public CellEditor createEditor(Composite composite)
{
TextCellEditor textEditor = new TextCellEditor(composite);
((Text) textEditor.getControl()).setTextLimit(10);
((Text) textEditor.getControl()).addVerifyListener(new DateCharacterVerifyListener());
((Text) textEditor.getControl()).setTextLimit(20);
return textEditor;
}

@Override
public final Object getValue(Object element) throws Exception
{
return ((LocalDate) descriptor().getReadMethod().invoke(adapt(element))).toString();
LocalDate date = (LocalDate) descriptor().getReadMethod().invoke(adapt(element));
return formatter.format(date);
}

@Override
Expand All @@ -58,7 +49,7 @@ public final void setValue(Object element, Object value) throws Exception

try
{
newValue = LocalDate.parse(String.valueOf(value));
newValue = LocalDate.parse(String.valueOf(value), formatter);
}
catch (DateTimeParseException e)
{
Expand Down
Expand Up @@ -266,7 +266,7 @@ public void exportMergedSecurityPrices(File file, List<Security> securities) thr

if (hasValues)
{
printer.print(Values.Date.format(pointer));
printer.print(pointer.toString());

for (ii = 0; ii < indices.length; ii++)
{
Expand Down
Expand Up @@ -3,6 +3,8 @@
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public abstract class Values<E>
{
Expand Down Expand Up @@ -144,10 +146,12 @@ public String format(Integer index)

public static final Values<LocalDate> Date = new Values<LocalDate>("yyyy-MM-dd", 1D, 1) //$NON-NLS-1$
{
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);

@Override
public String format(LocalDate date)
{
return String.format("%tF", date); //$NON-NLS-1$
return formatter.format(date);
}
};

Expand Down
Expand Up @@ -11,6 +11,9 @@
import java.util.Optional;
import java.util.function.Predicate;

import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVStrategy;

import name.abuchen.portfolio.Messages;
import name.abuchen.portfolio.math.Risk.Drawdown;
import name.abuchen.portfolio.math.Risk.Volatility;
Expand All @@ -25,9 +28,6 @@
import name.abuchen.portfolio.util.Interval;
import name.abuchen.portfolio.util.TradeCalendar;

import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVStrategy;

public class PerformanceIndex
{
private final Client client;
Expand Down Expand Up @@ -281,7 +281,7 @@ private void exportTo(File file, Predicate<Integer> filter) throws IOException
if (!filter.test(ii))
continue;

printer.print(Values.Date.format(dates[ii]));
printer.print(dates[ii].toString());
printer.print(Values.Amount.format(totals[ii]));
printer.print(Values.Amount.format(transferals[ii]));
printer.print(Values.Percent.format(delta[ii]));
Expand Down
Expand Up @@ -9,7 +9,6 @@

import name.abuchen.portfolio.Messages;
import name.abuchen.portfolio.model.Transaction;
import name.abuchen.portfolio.money.Values;
import name.abuchen.portfolio.util.Interval;

public abstract class ReportingPeriod
Expand Down Expand Up @@ -213,8 +212,7 @@ public FromXtoY(LocalDate startDate, LocalDate endDate)
@Override
public void writeTo(StringBuilder buffer)
{
buffer.append(CODE).append(Values.Date.format(getStartDate())).append('_')
.append(Values.Date.format(getEndDate()));
buffer.append(CODE).append(getStartDate().toString()).append('_').append(getEndDate().toString());
}

@Override
Expand Down Expand Up @@ -242,7 +240,7 @@ public SinceX(LocalDate startDate)
@Override
public void writeTo(StringBuilder buffer)
{
buffer.append(CODE).append(Values.Date.format(getStartDate()));
buffer.append(CODE).append(getStartDate().toString());
}

@Override
Expand Down

0 comments on commit dd4dc31

Please sign in to comment.