I know I can get the date from a datepicker using the following methods:
datepicker1.getDayOfMonth()
datepicker2.getMonth()
datepicker3.getYear()
I am also getting the time from a timepicker using the following methods:
timepicker1.getCurrentHour()
timepicker2.getCurrentMinute()
I need to format this data into "yyyy-MM-dd HH:mm:ss" format to place it into an sqlite database as a DATETIME variable. Is there a method which can handle this?
I think I will have to build it manually like so:
if(month < 10){
month = "0" + month;
}
if(day < 10){
day = "0" + day ;
}
The problem was that the months and days were single digit.
SimpleDateFormat outputFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date ModifiedDate = outputFormat.parse(strdate);
Simple as below:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
dateFormat.parse(yourDateHere);
You can get Current Date by this following and save it to the string
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
now pass this string to the Simpledateformat class and define your desired Format like following
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String currentDateandTime = sdf.format(mydate);
Related
I want today's date in my application to store it in a database as a string but when i used simpledatefromat and calender.getInstance(), it's not giving me proper date. The code is as below.
I tried simpledateformat's another constructor which takes locale as parameter but still not showing proper date
Date date = Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
String dateF = dateFormat.format(date);
expected date should be 21-04-2019
actual result 21-26-2019
To get the month like the way that you want, you need to change "dd-mm-yyyy" to "dd-MM-yyyy"
Date date = Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
String dateF = dateFormat.format(date);
I want to get current time specifically in English to save it in Database,
to get Current time i use function
private String get_current_Time() {
String CURRENT_TIME_FORMAT = "yyyy_MM_dd_HH_hh_mm_ss_a_MMMM_MMM_EEEE_EE";
return (String) DateFormat.format(CURRENT_TIME_FORMAT, Calendar.getInstance().getTime());
}
but when i set Locale to different language it gives me current time in that language.
for example if i set
conf.setLocale(new Locale("mr"));
it gives me date in marathi. I specifically want it in English. How to do it.
And Also how to change the language of Date once it is saved.I mean if I have saved date in English and while display i want that date to be shown in some other language, how to do it.?
As ADM suggested. new function that worked
public String get_current_Time() {
String CURRENT_TIME_FORMAT = "yyyy_MM_dd_HH_hh_mm_ss_a_MMMM_MMM_EEEE_EE";
SimpleDateFormat dateFormat = new SimpleDateFormat(CURRENT_TIME_FORMAT, Locale.ENGLISH);
return dateFormat.format(Calendar.getInstance().getTime());
}
So now it always returns Date in English
This should fix your issue! Try any of these
here is a simple tutorial
For java.util.Date, just create a new Date()
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
log.d(dateFormat.format(date)); //2016/11/16 12:08:43
For java.util.Calendar, uses Calendar.getInstance()
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
log.d(dateFormat.format(cal)); //2016/11/16 12:08:43
For java.time.LocalDateTime, uses LocalDateTime.now()
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
log.d(dtf.format(now)); //2016/11/16 12:08:43
For java.time.LocalDate, uses LocalDate.now()
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = LocalDate.now();
log.d(dtf.format(localDate)); //2016/11/16
my date format is display like this :
Date : 19/01/2015(January)
Here is the code :
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
String day = cal.getDisplayName(dayOfWeek, Calendar.LONG, Locale.US);
But I want to display in like this :
Date : 19/01/2015(Mon)
How can i modify the codes? Please help me take a look.Thanks!
You can do it standard Java way
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy(EEE)")
dateFormat.format(new Date())
Please use below three lines:
String format = "dd-MM-yyyy(EEE)";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
System.out.format("%30s %s\n", format, sdf.format(new Date(0)));
Logs - 01-21 16:39:34.125: I/System.out(11052): dd-MM-yyyy(EEE) 01-01-1970(Thu)
I am inserting the date to database like this:
long d = cal.getTimeInMillis();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
String time_string_f=dateFormat.format(d);
time_string_f is the string to insert in database , and the output is like:
07/09/2015 20:47:00
I want to get it from database and format it to be 12 hours with am/pm.
So I got this solution from here
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd-MM-yyyy hh:mm:ss.SSa");
DateTime jodatime = dtf.parseDateTime(string_date_from_database);
int yy= jodatime.getYear();
I am getting the year just to check if it works.
but it does not work and gives me this error:
07-09 22:34:48.399: I/FFFFF(7165): Invalid format: "07/09/2015 20:47:00" is malformed at "/09/2015 20:47:00"
Value in you database has format MM/dd/yyyy HH:mm:ss - without AM/PM suffix.
So you should parse is without a option and then convert date to 12 hours format.
Example:
String string_date_from_database = "07/09/2015 20:47:00";
DateTimeFormatter dtf = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
DateTime jodatime = dtf.parseDateTime(string_date_from_database);
String dateIn12HourFormat = jodatime.toString("MM/dd/yyyy hh:mm:ssa");
// Now 'dateIn12HourFormat' looks like `07/09/2015 08:47:00PM`
You can use simple utility method:
static final DateTimeFormatter hours24 = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
static final DateTimeFormatter hours12 = DateTimeFormat.forPattern("MM/dd/yyyy hh:mm:ssa");
static String convertTo12HoursFormat(String format24hours)
{
return hours12.print(hours24.parseDateTime(format24hours));
}
Usage:
String string_date_from_database = "07/09/2015 20:47:00";
String dateIn12HourFormat = convertTo12HoursFormat(string_date_from_database);
Try like the following.
long d = cal.getTimeInMillis();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
String time_string_f=dateFormat.format(d);
I have 2 dates in string format like DD/MM/YYYY. Now I have to write the condition if date1 is before date2. How can I do this? Please help me.
Try this:
SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = curFormater.parse(date1Str);
Date date2 = curFormater.parse(date2Str);
if (date1.before(date2))
{
}