Help with getting the date of the Android device. There is this code here, I want it to load a certain .xml file (markup) after receiving the date. I know that you need to use the if statement, how to attach it correctly here? Android Studio

private String getDate() { SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy hh:mm"); return dateFormat.format(new Date()); } 
  • What is xml markup? Try to formulate your question so that it is understandable to people who do not know anything about your problem, without the help of psychics - pavlofff
  • Yes, in Android Studio, so how can this be done? - George Botnar
  • The @GeorgeBotnar question is unclear. formulate it otherwise you will be deservedly zaminusuyut - Jiraff537
  • It is completely unclear from your question what exactly your problem is and what you want to get in the end. You do not understand the syntax of the if statement? You can not make a condition? Is the problem still with the if statement or getting the date? and what date? or what .. This is not a psychic forum, to guess your difficulties, arrange the question so that it is understandable to people who have no idea that you are not working. - pavlofff
  • I have an xml file (markup), I need to get a date from the device to load this xml. - George Botnar

1 answer 1

If you need to spin from the date in the text format, you can:

 if (getDate().equals("01.01.2017 06:00")) { // some actions } 

Alternatively, you can use the date representation in milliseconds:

 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy hh:mm"); String currentDateString = getDate(); long currentDateInMills = simpleDateFormat.parse(currentDateString).getTime(); String someDateString = "01.01.2017 06:00"; long someDateInMills = simpleDateFormat.parse(someDateString).getTime(); if (currentDateInMills == someDateInMills) { // some actions } 
  • Now I have it, but the application running on the smartphone crashes. pastebin.com/7dwwjhdc - George Botnar
  • @GeorgeBotnar, Show the stack trace. - post_zeew