Hi in the below code want to remove T and Zone from the LocalDateTime .
Can any one help me with this issue.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
LocalDateTime time = LocalDateTime.now();
firstDayOfQuarteropp = time.with(time.getMonth().firstMonthOfQuarter())
.with(TemporalAdjusters.firstDayOfMonth());
Log.d("firstDayOfQuarteropp", String.valueOf(firstDayOfQuarteropp));
lastDayOfQuarteropp = firstDayOfQuarteropp.plusMonths(2)
.with(TemporalAdjusters.lastDayOfMonth());
Log.d("lastDayOfQuarteropp", String.valueOf(lastDayOfQuarteropp));
}
Actual output:
2020-04-01T11:54:05.514
Expected output:
2020-04-01 11:54:05
You need to use DateTimeFormatter along with LocalDateTime
LocalDateTime time = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formatDateTime = time.format(formatter);
Log.d("FormattedTime", "Time: " + formatDateTime);
You have to format the local date and time.
public String formatedDate() {
LocalDateTime time = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return time.format(formatter);
}
And use it as:
String dt=formatedDate();
Related
I have the following String:
18/07/2019 16:20
I try to convert this string into LocalDateTime with the following code:
val stringDate = expiration_button.text.toString()
val date = LocalDateTime.parse(stringDate, DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm")).toString()
java.time.format.DateTimeParseException: Text '18/07/2019 04:30:00'
could not be parsed: Unable to obtain LocalDateTime from
TemporalAccessor
What I'm missing?
I think this will answer your question:
val stringDate = expiration_button.text.toString()
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm");
val dt = LocalDate.parse(stringDate, formatter);
Edit 1:
It's probably crashing because you are using a 12hr Hour, instead of a 24hr pattern.
Changing the hour to 24hr pattern by using a capital H should fix it:
val dateTime = LocalDateTime.parse(stringDate, DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"));
Use below to convert the time from String to LocalDateTime, but make sure you are getting the time in String form.
String str = "2016-03-04 11:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
Btw, If your String contains seconds as well like "2016-03-04 11:30: 40", then you can change your date time format to yyyy-MM-dd HH:mm:ss" as shown below:
String str = "2016-03-04 11:30: 40";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
Change your datetime format to "dd/MM/yyyy hh:mm a" and provide a string date with additional AM/PM information, e.g. val stringDate = "18/07/2019 04:20 PM" or just use the 24 hour format "dd/MM/yyyy HH:mm".
You may try using "-" instead of "/" on the 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
I want to know if there is a way to get the format that is currently being used by the system.
As of now i am using the Joda Time library and manually specifying the foramt that i expect the date to be in.
private final DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
LocalDate date = LocalDate.parse(event_date, formatter);
This is of course not the best idea to hardcode the pattern,so is there a way i can get the pattern from the system?
You can use something like this:
DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
String pattern = ((SimpleDateFormat)formatter).toPattern();
String localPattern = ((SimpleDateFormat)formatter).toLocalizedPattern();
You can do like this:
String datePattern;
String timePattern;
void loadDateTimePattern(Context context) {
SimpleDateFormat dateFormat = (SimpleDateFormat) DateFormat.getDateFormat(context);
SimpleDateFormat timeFormat = (SimpleDateFormat) DateFormat.getTimeFormat(context);
datePattern = dateFormat.toPattern();
timePattern = timeFormat.toPattern();
}
DateTimeFormatter timeFormatter = DateTimeFormat.forPattern(timePattern);
DateTimeFormatter timeFormatterFull = DateTimeFormat.forPattern(datePattern + ", " + timePattern);
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 the following String.
21-Mar-2014
How can i convert this into a valid Joda DateTime object?
I've tried the following with no joy:
DateTimeFormatter formatter = DateTimeFormat.forPattern("d-MMM/Y");
DateTime dt = formatter.parseDateTime(date);
Thanks in advance
Matt
There are already so many question in SO related to this.
Check this.
A genuine joda answer with corrected pattern string and explicit Locale:
String input = "21-Mar-2014";
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd-MMM-yyyy").withLocale(Locale.ENGLISH);
DateTime dt = dtf.parseDateTime(input); // using the default time zone
System.out.println(dt); // 2014-03-21T00:00:00.000+01:00 (my zone: Europe/Berlin)
If you don't need time part (regarding your input!) then I recommend to use:
LocalDate date = dtf.parseLocalDate(input);
String dateInString = "7-Jun-2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
you can try this
Date date1;
String myFormatString = "MMM-dd-yyyy"; // for example
String Your_Date=txtDate.getText().toString();
SimpleDateFormat df = new SimpleDateFormat(myFormatString);
date1 = df.parse(Your_Date);
you just use the following simple date format
SimpleDateFormat sdf=new SimpleDateFormat("d-MMM-yyyy");
Date date1=sdf.parse("21-Mar-2014");
Try this
DateTimeFormatter formatter = DateTimeFormat.forPattern("d-MMM-yyyy");
DateTime dt = formatter.parseDateTime(date);
System.out.println(dt.toString());