Java algorithm UNIX timestamp conversion

Source: Internet
Author: User
Tags greatest common divisor

Import Java.util.Scanner;
public class Timel {

private static Scanner in;

public static void Main (string[] args) {//Implement the conversion of the current system timestamp, enter a valid time to convert
SYSTEM.OUT.PRINTLN ("Time Machine system open, current Beijing times:");
TIMECP now=new TIMECP (System.currenttimemillis ()/1000);
System.out.println (Now.cptime ());
System.out.println ("Please enter the time distance you want to go (1970 is the starting point):");
in = new Scanner (system.in);
Long Tp=in.nextlong ();
TIMECP some=new TIMECP (TP);
System.out.println (Some.cptime ());
System.out.print ("You Cross to");
System.out.println (Some.cptime () + "see other areas of Time yes->1");
int A=in.nextint ();
if (a==1)
{
System.out.println (Some.timearea (Some.cptime ()));
}
}
}
Class TIMECP
{
Private long time;
Public TIMECP (long x)
{
Time=x;
}
Public long[] Cptime ()//constructor, convert timestamp to Month day ... Format
{
Long year=1970,month=1,day=1,hour=0,minute=0,second=0;
second=time%60;//is a very useful technique in the algorithm, from the odd even to the discriminant of the prime number, from the modulo exponentiation to the greatest common divisor, the monkey chose the King (abstract it can be realized by the loop)
minute=time%3600/60;//less than an hour of seconds as minutes
Hour= (time+86400)%86400/3600+8;
day=time/86400+1;
while (DAY>=32)//Because the number of days per month is not too large (there are found friends can be optimized), so use if for the year cycle. The main idea is to reduce the number of days, years plus, insufficient to quit, there is a leap year problem
{
day=day-31;
month++;
if (((year%4) ==0&& (year%100)!=0) | | (year%400==0))
{
if (day>=30)
{
day=day-29;
month++;
}
}
Else
{
if (day>=29)
{
day=day-28;
month++;
}

}
if (day>=32)
{
day=day-31;
month++;
}
if (day>=31)
{
day=day-30;
month++;
}
if (day>=32)
{
day=day-31;
month++;
}
if (day>=31)
{
day=day-30;
month++;
}
if (day>=32)
{
day=day-31;
month++;
}
if (day>=32)
{
day=day-31;
month++;
}
if (day>=31)
{
day=day-30;
month++;
}
if (day>=32)
{
day=day-31;
month++;
}
if (day>=31)
{
day=day-30;
month++;
}
if (day>=32)
{
day=day-31;
month++;
month=1;
year++;
}
}
System.out.println (year+ "-" +month+ "-" +day+ "" +hour+ ":" +minute+ ":" +second ");
Long[] Tmp={year,month,day,hour,minute,second};
return TMP;
}
Public long Timearea (long[] x)//time zone conversion
{
SYSTEM.OUT.PRINTLN ("zone seven" +x[0]+ "-" +x[1]+ "-" +x[2]+ "" + (X[3]-1) + ":" +x[4]+ ":" +x[5]);
System.out.println ("Six zones" +x[0]+ "-" +x[1]+ "-" +x[2]+ "" + (X[3]-2) + ":" +x[4]+ ":" +x[5]);
System.out.println ("Five districts" +x[0]+ "-" +x[1]+ "-" +x[2]+ "" + (x[3]-3) + ":" +x[4]+ ":" +x[5]);
System.out.println ("Four districts" +x[0]+ "-" +x[1]+ "-" +x[2]+ "" + (X[3]-4) + ":" +x[4]+ ":" +x[5]);
System.out.println ("Three districts" +x[0]+ "-" +x[1]+ "-" +x[2]+ "" + (X[3]-5) + ":" +x[4]+ ":" +x[5]);
System.out.println ("Two districts" +x[0]+ "-" +x[1]+ "-" +x[2]+ "" + (X[3]-6) + ":" +x[4]+ ":" +x[5]);
System.out.println ("One zone" +x[0]+ "-" +x[1]+ "-" +x[2]+ "" + (X[3]-7) + ":" +x[4]+ ":" +x[5]);
System.out.println ("The area" +x[0]+ "-" +x[1]+ "-" +x[2]+ "" + (X[3]-8) + ":" +x[4]+ ":" +x[5]);
return 0;
}

Java algorithm UNIX timestamp conversion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.