Last active
April 18, 2022 09:06
-
-
Save paolorotolo/2e25b8f91874d83d6382 to your computer and use it in GitHub Desktop.
Split a DateTime string to get single elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.DateFormat; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
// Use example: | |
// DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); | |
// SplitDateTime splitDateTime = new SplitDateTime("2015-09-01 14:05"); | |
// timeEditText.setText(splitDateTime.getHour + ":" + splitDateTime.getMinute); | |
// dateEditText.setText(splitDateTime.getDay + "/" + splitDateTime.getMonth + "/" + splitDateTime.getDay); | |
public class SplitDateTime { | |
String origDateTime; // Example "yyyy-MM-dd HH:mm" | |
DateFormat inputFormat; | |
public SplitDateTime(String origDatetime, DateFormat origDateFormat) { | |
this.origDateTime = origDatetime; | |
this.inputFormat = origDateFormat; | |
} | |
public String getHour(){ | |
DateFormat finalFormat = new SimpleDateFormat("HH"); | |
Date parsed = null; | |
try { | |
parsed = inputFormat.parse(origDateTime); | |
} catch (ParseException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return finalFormat.format(parsed); | |
} | |
public String getMinute(){ | |
DateFormat finalFormat = new SimpleDateFormat("mm"); | |
Date parsed = null; | |
try { | |
parsed = inputFormat.parse(origDateTime); | |
} catch (ParseException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return finalFormat.format(parsed); | |
} | |
public String getYear(){ | |
DateFormat finalFormat = new SimpleDateFormat("yyyy"); | |
Date parsed = null; | |
try { | |
parsed = inputFormat.parse(origDateTime); | |
} catch (ParseException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return finalFormat.format(parsed); | |
} | |
public String getMonth(){ | |
DateFormat finalFormat = new SimpleDateFormat("MM"); | |
Date parsed = null; | |
try { | |
parsed = inputFormat.parse(origDateTime); | |
} catch (ParseException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return finalFormat.format(parsed); | |
} | |
public String getDay(){ | |
DateFormat finalFormat = new SimpleDateFormat("dd"); | |
Date parsed = null; | |
try { | |
parsed = inputFormat.parse(origDateTime); | |
} catch (ParseException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return finalFormat.format(parsed); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment