Fix 12 am/pm

This commit is contained in:
Orange_Dugongo 2016-10-21 13:19:12 +02:00
parent aba69b19bf
commit dcf8057269
4 changed files with 17 additions and 12 deletions

Binary file not shown.

View File

@ -2,7 +2,7 @@ import java.util.Scanner;
public class Test{ public class Test{
public static void main(String [] args){ public static void main(String [] args){
Time t = new Time(12, 50, "am"); Time t = new Time(11, 00, "am");
Time t2 = new Time(1, 1, "pm"); Time t2 = new Time(1, 1, "pm");
Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(System.in);

Binary file not shown.

View File

@ -53,10 +53,18 @@ public class Time{
return null; return null;
} }
private boolean isMidday(){
return h==12 && id.equals("pm");
}
private boolean isMidnight(){
return h==12 && id.equals("am");
}
public int toMinute(){ public int toMinute(){
if(id.equals("pm")) if((id.equals("pm") && !isMidday()) || isMidnight())
h+=12; h+=12;
return h*60+m; return (h*60+m)%(24*60);
} }
public boolean isAfter(Time t){ public boolean isAfter(Time t){
@ -71,7 +79,11 @@ public class Time{
int totm=this.toMinute() + m%(24*60); int totm=this.toMinute() + m%(24*60);
this.m=totm%60; this.m=totm%60;
this.h=(totm/60)%24; this.h=(totm/60)%24;
if(this.h>12){ if(this.h==0){
this.h+=12;
}else if(this.h==12){
this.id="pm";
}else if(this.h>12){
this.id="pm"; this.id="pm";
this.h-=12; this.h-=12;
}else }else
@ -79,14 +91,7 @@ public class Time{
} }
public void shift(int h, int m){ public void shift(int h, int m){
int totm=this.toMinute() + (m+h*60)%(24*60); shift(h*60+m);
this.m=totm%60;
this.h=(totm/60)%24;
if(this.h>12){
this.id="pm";
this.h-=12;
}else
this.id="am";
} }