72 lines
1.8 KiB
Java
72 lines
1.8 KiB
Java
package calcul;
|
|
|
|
import java.text.ParseException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Date;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
import javax.swing.JOptionPane;
|
|
|
|
public class formatDateWriter {
|
|
|
|
/**
|
|
* Retourne true sie le format est YYYY-MM-JJTHH:MM:SS.<br>
|
|
* @param date une String.
|
|
* @return retrourne true ou false.
|
|
*/
|
|
public static boolean isCorrect(String date) {
|
|
if(date.isEmpty()) return true;
|
|
|
|
Pattern p = Pattern.compile("^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$");
|
|
Matcher m = p.matcher(date);
|
|
if(m.find()) return true;
|
|
JOptionPane.showMessageDialog(null, "Le format de la date "+ date +" n'est pas correct.");
|
|
return false;
|
|
}
|
|
|
|
|
|
/**
|
|
* Retourne une date à partir d'une String composée de YYYY-MM-JJTHH:MM:SS.<br>
|
|
* @param libreoffice_date
|
|
* @return une date.
|
|
*/
|
|
public static Date DateLibreOffice(String libreoffice_date){
|
|
boolean contientHeure = false;
|
|
if(libreoffice_date.contains("T")) {
|
|
libreoffice_date=libreoffice_date.replace("T", " ");
|
|
contientHeure=true;
|
|
}
|
|
Date d = null;
|
|
SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
|
|
|
|
if(!contientHeure) simpledateformat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
try {
|
|
d = simpledateformat.parse(libreoffice_date);
|
|
}catch(ParseException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return d;
|
|
}
|
|
|
|
/**
|
|
* Retourne une String à partir d'une date composé de YYYY-MM-JJTHH:MM:SS.</br>
|
|
* @param d
|
|
* @return
|
|
* @throws ParseException
|
|
*/
|
|
public static String DateLibreOffice(Date d) throws ParseException {
|
|
SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
|
|
String d1 = simpledateformat.format(d);
|
|
|
|
return d1;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|