Skip to content

Instantly share code, notes, and snippets.

@evik246
Last active April 5, 2022 19:47
Show Gist options
  • Save evik246/57ba171eca1866ee9414a2397e7b8e55 to your computer and use it in GitHub Desktop.
Save evik246/57ba171eca1866ee9414a2397e7b8e55 to your computer and use it in GitHub Desktop.
Показывает время до начала занятий
package com.example.timebutton;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
static Random random;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
random = new Random();
}
public void showTime(View view) throws ParseException {
//цвет заголовка
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.rgb
(random.nextInt(255),
random.nextInt(255),
random.nextInt(255))));
//расчет времени
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
long lessonDate = format.parse("09.04.2022 13:30:00").getTime();
long difference = lessonDate - System.currentTimeMillis();
long seconds = difference / 1000 % 60;
long minutes = difference / (60 * 1000) % 60;
long hours = difference / (60 * 60 * 1000) % 24;
long days = difference / (24 * 60 * 60 * 1000);
//вывод времени
setTitle(String.format("%d days %02d:%02d:%02d", days, hours, minutes, seconds));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment