Skip to content

Commit

Permalink
Factorisation code formatage date
Browse files Browse the repository at this point in the history
  • Loading branch information
AnaelMobilia committed Nov 28, 2023
1 parent a3a6a5e commit 8d3775c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/com/pcinpact/CommentairesActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
import com.pcinpact.network.AsyncHTMLDownloader;
import com.pcinpact.network.RefreshDisplayInterface;
import com.pcinpact.utils.Constantes;
import com.pcinpact.utils.MyDateUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -469,13 +469,13 @@ public void downloadHTMLFini(String uneURL, ArrayList<? extends Item> desItems)
*/
private void majDateRefresh() {
// Date de dernier refresh
long dernierRefresh = TimeUnit.SECONDS.toMillis(monDAO.chargerDateRefresh(idArticle));
long dernierRefresh = monDAO.chargerDateRefresh(idArticle);

if (dernierRefresh == 0) {
// Jamais synchro...
headerTextView.setText(getString(R.string.lastUpdateNever));
} else {
String monTexte = getString(R.string.lastUpdate) + new SimpleDateFormat(Constantes.FORMAT_DATE_DERNIER_REFRESH, Constantes.LOCALE).format(dernierRefresh);
String monTexte = getString(R.string.lastUpdate) + MyDateUtils.formatDate(Constantes.FORMAT_DATE_DERNIER_REFRESH, dernierRefresh);
// Une MàJ à déjà été faite
headerTextView.setText(monTexte);
}
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/com/pcinpact/ListeArticlesActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import com.pcinpact.utils.Constantes;
import com.pcinpact.utils.MyDateUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -606,13 +605,13 @@ private ArrayList<Item> prepareAffichage() {
}

// MàJ de la date de dernier refresh
long dernierRefresh = TimeUnit.SECONDS.toMillis(monDAO.chargerDateRefresh(Constantes.DB_REFRESH_ID_LISTE_ARTICLES));
long dernierRefresh = monDAO.chargerDateRefresh(Constantes.DB_REFRESH_ID_LISTE_ARTICLES);

if (dernierRefresh == 0) {
// Jamais synchro...
headerTextView.setText(getString(R.string.lastUpdateNever));
} else {
String monTexte = getString(R.string.lastUpdate) + new SimpleDateFormat(Constantes.FORMAT_DATE_DERNIER_REFRESH, Constantes.LOCALE).format(dernierRefresh);
String monTexte = getString(R.string.lastUpdate) + MyDateUtils.formatDate(Constantes.FORMAT_DATE_DERNIER_REFRESH, dernierRefresh);
// Une MàJ à déjà été faite
headerTextView.setText(monTexte);
}
Expand Down
22 changes: 4 additions & 18 deletions app/src/main/java/com/pcinpact/items/ArticleItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@
package com.pcinpact.items;

import com.pcinpact.utils.Constantes;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import com.pcinpact.utils.MyDateUtils;

/**
* Objet Article.
Expand Down Expand Up @@ -95,11 +91,7 @@ public int getType() {
* @return Heure & minute de la publication
*/
public String getHeureMinutePublication() {
Date maDate = new Date(TimeUnit.SECONDS.toMillis(this.getTimeStampPublication()));
// Format souhaité
DateFormat dfm = new SimpleDateFormat(Constantes.FORMAT_AFFICHAGE_ARTICLE_HEURE, Constantes.LOCALE);

return dfm.format(maDate);
return MyDateUtils.formatDate(Constantes.FORMAT_AFFICHAGE_ARTICLE_HEURE, this.getTimeStampPublication());
}

/**
Expand All @@ -108,15 +100,9 @@ public String getHeureMinutePublication() {
* @return Date de la publication
*/
public String getDatePublication() {
Date maDate = new Date(TimeUnit.SECONDS.toMillis(this.getTimeStampPublication()));
// Format souhaité
DateFormat dfm = new SimpleDateFormat(Constantes.FORMAT_AFFICHAGE_SECTION_DATE, Constantes.LOCALE);
String laDate = dfm.format(maDate);

String laDate = MyDateUtils.formatDate(Constantes.FORMAT_AFFICHAGE_SECTION_DATE, this.getTimeStampPublication());
// Première lettre en majuscule
laDate = String.valueOf(laDate.charAt(0)).toUpperCase(Constantes.LOCALE) + laDate.substring(1);

return laDate;
return String.valueOf(laDate.charAt(0)).toUpperCase(Constantes.LOCALE) + laDate.substring(1);
}

/**
Expand Down
24 changes: 19 additions & 5 deletions app/src/main/java/com/pcinpact/utils/MyDateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -64,15 +65,28 @@ public static long convertToTimestamp(String uneDate) {
}

/**
* Convertit un timestamp en date ISO8601
* Convertir un timestamp en date ISO8601
*
* @param unTimestamp timestamp
* @return String
*/
public static String convertToDateISO8601(long unTimestamp) {
DateFormat dfm = new SimpleDateFormat(Constantes.FORMAT_DATE, Constantes.LOCALE);
public static String convertToDateISO8601(final long unTimestamp) {
return formatDate(Constantes.FORMAT_DATE, unTimestamp);
}

/**
* Formatter une date
*
* @param format format souhaité (SimpleDateFormat)
* @param unTimestamp un timestamp
* @return String
*/
public static String formatDate(final String format, final long unTimestamp) {
Date maDate = new Date(TimeUnit.SECONDS.toMillis(unTimestamp));
// Format souhaité
DateFormat dfm = new SimpleDateFormat(format, Constantes.LOCALE);
dfm.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
return dfm.format(unTimestamp * 1000L);
return dfm.format(maDate);
}

/**
Expand All @@ -81,7 +95,7 @@ public static String convertToDateISO8601(long unTimestamp) {
* @param nbJours nombre de jours à enlever
* @return timestamp
*/
public static long timeStampDateActuelleMinus(int nbJours) {
public static long timeStampDateActuelleMinus(final int nbJours) {
long monRetour;

// Date du jour
Expand Down

0 comments on commit 8d3775c

Please sign in to comment.