I have a String "26/09/2012 07:30:00" is it possible to create a new DateTime based on this String? Eventually i just want the time eg 7:30. I am going to format the DateTime by using DateFormatter eg
DateTimeFormatter fmt = DateTimeFormat.forPattern("k,m");
My question is how to construct a DT from the orginal String, i can format it once it's a DT.
Create an appropriate DateTimeFormatter for the input format, and then call DateTimeFormatter.parseLocalDateTime. (LocalDateTime is more appropriate than DateTime here, as your input data doesn't have a time zone or UTC offset indicator. You can convert to DateTime if you really need to, but it sound like you don't.)
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String formattedDate = df.format(c.getTime());
Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();
// Now we display formattedDate value in TextView
TextView txtView = new TextView(this);
txtView.setText("Current Date and Time : "+formattedDate);
txtView.setGravity(Gravity.CENTER);
txtView.setTextSize(20);
setContentView(txtView);
Related
// For Date validation
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
String datechosen = dateText.getText().toString() ;
Date dateselected = simpleDateFormat1.parse(datechosen);
System.out.println(dateselected);
// For Time validation
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
String timechosen = timeText.getText().toString();
Date timeselected = simpleDateFormat.parse(timechosen);
Calendar cal = Calendar.getInstance();
cal.setTime(timeselected);
cal.add(Calendar.HOUR, noofhourselected);
cal.add(Calendar.MINUTE, noofminselected);
timeselected = cal.getTime();
System.out.println(timeselected);
I am working on converting the string which i have into the date and time format. For example, the string datechosen contain "26/10/2020". I am able to to convert it into date format and print it out.
But for the time string, i am unable to print them out. I am facing the error below:
Screenshot of the log message
But if i swap the position of the codes the other way round,
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
String timechosen = timeText.getText().toString();
Date timeselected = simpleDateFormat.parse(timechosen);
Calendar cal = Calendar.getInstance();
cal.setTime(timeselected);
cal.add(Calendar.HOUR, noofhourselected);
cal.add(Calendar.MINUTE, noofminselected);
timeselected = cal.getTime();
System.out.println(timeselected);
// For Date validation
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
String datechosen = dateText.getText().toString() ;
Date dateselected = simpleDateFormat1.parse(datechosen);
System.out.println(dateselected);
The time will be printed instead
These are the two input fields
You are setting date in wrong format for time. for cal.setTime(timeselected); setTime takes Date Refer java doc
Use same format as used for Date.
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
I would like to convert this time stamps to Long value.
2016-07-13T21:11:45+00:00
I still don't know what the format of above time stamp, like MM/dd/yyyy hh:mm:ss aa to use with the SimpleDateFormat.
Try this code to
String timeStr = "2016-11-01T09:45:00.000+02:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date dateObj= sdf.parse(timeStr);
System.out.println(dateObj.getTime());
It would be yyyy-MM-dd\'T\'HH:mm:ss and you can convert it to long by:
public static long convertDateToMilliseconds(String fromFormat, String sourceDate) {
SimpleDateFormat sdfFrom = new SimpleDateFormat(fromFormat);
//SimpleDateFormat sdfTo = new SimpleDateFormat(toFormat);
Date date = sdfFrom.parse(sourceDate);
//String convertedDate = sdfTo.format(date);
return date.getTime();
}
Use this link to find in which formate to create Simple date formater then create date object with that date formater. Then you can get time in millis like below
long millisecond = beginupd.getTime();
SimpleDateFormat almost states this in its examples. For that you can use
"yyyy-MM-dd'T'HH:mm:ssXXX"
Hello I want to display current date. I trying do it like this:
calendar c = Calendar.getInstance();
System.out.println("Current time => "+c.getTime());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = df.format(c.getTime());
TextView txtView = (TextView)findViewById(R.id.tvDate);
txtView.setText("Current Date and Time : "+formattedDate);
But it don't display the date in my textView.
Is there a better way to display the same date? I want like this: Monday, April 22
You should look at the documentation for SimpleDateFormat.
SimpleDateFormat
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
should be
SimpleDateFormat df = new SimpleDateFormat("EEEE, MMMMM dddd");
Date now = new Date();
String str = DateUtils.getRelativeDateTimeString(
this, // Suppose you are in an activity or other Context subclass
now.getTime(), // The time to display
MINUTE_IN_MILLIS, // The resolution. This will display only minutes
// (no "3 seconds ago"
WEEK_IN_MILLIS, // The maximum resolution at which the time will switch
// to default date instead of spans. This will not
// display "3 weeks ago" but a full date instead
0); // Eventual flags
Other values for MINUTE_IN_MILLIS and YEAR_IN_MILLIS include:
SECOND_IN_MILLIS
MINUTE_IN_MILLIS
HOUR_IN_MILLIS
DAY_IN_MILLIS
WEEK_IN_MILLIS
YEAR_IN_MILLIS
Any custom value in milliseconds
Then set text as
txtView.setText("Current Date and Time : "+now);