Date of last connection in Android studio app - android

I need the date of the last connection. When I enter again I have to see the last date that I entered in my app.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
Date date = new Date();
String fecha = dateFormat.format(date);
I have this code but I don't know how to continue.

If I understand your right you can just use System.currentTimeMillis()it looks like this.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
Date date = new Date(System.currentTimeMillis());
String fecha = dateFormat.format(date);
Then you can save this value everywhere you want

Related

Simpledateformat is not giving proper 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);

How Get Current time in English in Android?

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

Convert Date Instance in local timezone

I have a date instance, how to convert it into local time zone?
Date meetingStartDate = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT, Locale.ENGLISH).parse(model.StartDateTimeUtc);
You can use below code to get time in local time zone
Date meetingStartDate = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT, Locale.ENGLISH).parse(model.StartDateTimeUtc);
DateFormat formatter= new SimpleDateFormat("MM/dd/yyyy HH:mm:ss Z");
formatter.setTimeZone(TimeZone.getDefault());
System.out.println(formatter.format(meetingStartDate));
SimpleDateFormat df = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT, Locale.ENGLISH);
df.setTimeZone(TimeZone.getDefault());
df = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT, Locale.ENGLISH);
df.setTimeZone(TimeZone.getDefault());
date == null ? dateStr : df.format(meetingStartDate );

How to return current date in format "YYYY-mm-dd" as a Date datatype?

I know there are many ways to solve this but I am looking for a simple one line solution.
Typically it would be
new SimpleDateFormat("yyyy-MM-dd").format(new Date())
However this returns a string and I need an object of the Date type.
Using the GregorianCalendar class is okay too since it can return a Date type.
thanks
Try it out in this way:
String strDate = sdf.format(cal.getTime())
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf1 = new SimpleDateFormat();
sdf1.applyPattern("yyyy-MM-dd");
Date date = sdf1.parse(strDate);
Date dateObj = new SimpleDateFormat("yyyy-MM-dd").parse( new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
Date d = (new SimpleDateFormat("yyyy-MM-dd")).parse("2011-12-13");
or
Date now = new Date();

Getting the current time and date in the device's defined format

How can I get the current time and date according to phone device format?
my code is
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm aa",
Locale.ENGLISH);
String var = dateFormat.format(date));
It displays like 2PM instead of 14PM.
You can the device format data and time using
Date date = new Date();
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
Log.d(TAG,dateFormat.format(date)+");
Use calendar object instead of Date because it is deprecated now and by using caledar object you can get hour in 12 hours formate as
int hour = calendarObject.get(Calendar.HOUR);
for 24 formate as:
int hour = calendarObject.get(Calendar.HOUR_OF_DAY);
for further detail you can visit this
Just change "hh:mm aa" to "HH:mm aa"

Categories

Resources